$.ajax({
url: '/ajax/get_rule.php',
dataType: 'json',
data: {
feature : feature,
ajax : 1
},
success: function(resp) {
console.log('in here');
console.log('response: ' + resp);
},
error : function() {
console.log('there was an error...');
}
});
I am using the PHP PEAR Services_JSON class to encode my array into a JSON string and then echo that out in the response, like this:
echo Registry('json')->encode(array('ajax' => 1, 'feature' => 'rfc1872'));
exit;
In the above example Registry('json')
is a Services_JSON
object.
I have also tried the PHP built-in function json_encode
:
echo json_encode(array('ajax' => 1, 'feature' => 'rfc1872'));
exit;
Neither of these work and although I don't fall into the error callback in the jQuery ajax call above I get a response value of response: [object Object]
with an empty object in Firebug.
Why would I not receive a response back to my jQuery code with a JSON-encoded string?
console.log('response: ' + resp);
will convert the object to a string first, and the default string representation of an object is"[object Object]"
, no matter whether it has properties or not. What doesconsole.log(resp);
show you? If you didn't get JSON as response, the success callback would not even be executed I believe. – Felix Kling Apr 5 '13 at 9:34console.log('response: ' + resp);
toconsole.log(resp);
as you advised and the object pulls through correct. Many thanks, can you submit as an answer and i'll happily accept. Thanks for pointing this out. – crmpicco Apr 5 '13 at 9:44