Reputation: 82
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.
I hope this helps someone else.
Upvotes: 1
Views: 125
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
Reputation: 2230
json_encode
your output__construct
(not _construct
)$sService = $_POST['sService'];
should be $this->sService = $_POST['sService'];
new request();
Upvotes: 2