Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax post - json is returning but not working right

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?

like image 292
HorusKol Avatar asked Dec 10 '25 04:12

HorusKol


1 Answers

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:

$.post("/gallery/resize", "",
  function (data) {
    alert(data);
    alert(data.complete);
    if (data.complete) {
      alert("done");
    } else {
      alert("blah");
    }
  },
  "json"
);
like image 168
HorusKol Avatar answered Dec 12 '25 18:12

HorusKol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!