user2812097
user2812097

Reputation: 82

PHP echoing emptying string in $.ajax response

I'm trying to get back data from an AJAX call. I tested with a string but it's returning empty. I just want to print the string to console on response.

JS:

makeRequest: function(sService, oData){
        var request = $.post(
            '../../Utils/utils/php/userQuery.php'
            ,{'sService' : sService, 'oData':  oData}
            ,'json'
        );
        request.done(function(oResult){
            // This is firing off, but is printing empty string
            console.log(JSON.stringify(oResult));
            var fail = errorHandler.check(oResult);
            if(!fail){
                // This is firing off, but is printing empty string
                console.log(oResult);
            } else{
                console.log(fail);
            }
        });
        request.fail(function(oResult){
            console.log("There was an error in retrieving service data");
        });
    }

PHP:

class request{
    private $sService;
    function _construct($sService){
        $_SESSION['user_Id'] = 100000;
        $this->$sService = $sService;
        switch($this->$sService){
            case 'follower':
                break;
            case 'followee':
                break;
            case 'promoter':
                break;
            case 'promotee':
                break;
            case 'note':
                // This should be hit and return "hit"
                echo json_encode("hit");
                break;
            case 'createFollowee':
                break;
            case 'createPromotee':
                break;
            case 'createNote':
                break;
            default:
                echo "ErrorCode: 4000";
                break;
        }
    }
}
$request = new request($_POST['sService']);

If I try to $.parseJSON(oResult) the response I get an error with parsing because it's empty. Where am I going wrong at?

ANSWER:

The updated code above works. There was several problems provided by answers.

  1. I didn't add 2 _ to construct().[before: _construct after: __construct]
  2. I didn't is $this to point to my global variable inside of my class.
  3. I originally didn't initialize my class.
  4. I wasn't passing my variable(s) to the construct when initializing class.

I hope this helps someone else.

Upvotes: 1

Views: 125

Answers (2)

Sithu
Sithu

Reputation: 4862

This could be your class constructor not running properly. The correct syntax of declaring the constructor function is function __construct() (two underscores preceded) or function theClassName(). Check the PHP manual.

http://codepad.viper-7.com/k3vclY - Nothing/empty output with _construct() of one underscore
http://codepad.viper-7.com/vabqqL - Something output with __construct() of two underscores

Upvotes: 0

BreyndotEchse
BreyndotEchse

Reputation: 2230

  1. Use the dev-tools of your browser to check the server response. Maybe the response is really empty?
  2. You want to get JSON data, but you don't json_encode your output
  3. PHP constructors start with 2 underscores: __construct (not _construct)
  4. $sService = $_POST['sService']; should be $this->sService = $_POST['sService'];
  5. The constructor won't be called when you don't initialize your class with new request();

Upvotes: 2

Related Questions