I’ve started the following skeleton for an ajax/post/update function I’m wanting to write in javascript (using jquery):
$.post("/gallery/resize",
function (data) {
alert(data);
alert(data.complete);
if (data.complete) {
alert("done");
} else {
alert("blah");
}
},
"json"
);
And the response script on the server is:
$return['complete'] = 'complete';
header('Content-type: application/json');
echo json_encode($return);
exit;
The FireBug console shows that I get a JSON string in response – but the value of data.complete is ‘undefined’. Here is the string from the server as reported by the FireBug (I also have a corresponding value/data pair under the JSON tab under the XHR display in the console):
{"complete":"complete"}
Any pointers on what I might’ve missed…
I am working on a localhost server – apache on ubuntu – if that makes a difference?
oh boy – turns out that I was a bit too trusting of the power of jQuery – I was missing a parameter in the $.post() method which may be optional unless you want to specify the other things.
the odd thing is that the callback works without the preceding data parameter being set – but it freaks out when you want to set the datatype (and must have the data and callback set).
So – the correct code for what I want would be: