I have the following PHP code that returns a JSON object via Ajax to the client.
function loadAll(){
$knowledgeHandler = new KnowledgeLevelHandler();
$json = $knowledgeHandler->loadAll();
header('Content-type: application/json');
print $json;
}
I have a test function assigned to the success callback of jQuery Ajax.
successCallback = function(data){
alert("A");
}
options = {
"data": data,
"async": false,
"type": "post",
"success":successCallback
}
$.ajax(url, options);
When I remove the header('Content-type: text/json'); from PHP code, the callback is executed, but when it is executed, the success callback isn’t executed. What is wrong?
I believe you need to specify the returned data type as JSON in your JQuery AJAX call.
http://api.jquery.com/jQuery.ajax/
Your successCallback function will now contain a data object, through which you can access your variables; data.var_1, data.some_other_var, etc.
Neal