最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ajax post - json is returning but not working right - Stack Overflow

programmeradmin1浏览0评论

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(dataplete);
    if (dataplete) {
      alert("done");
    } else {
      alert("blah");
    }
  },
  "json"
);

And the response script on the server is:

$return['plete'] = 'plete';
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 dataplete 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):

{"plete":"plete"}

Any pointers on what I might've missed...

I am working on a localhost server - apache on ubuntu - if that makes a difference?

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.plete);
    if (data.plete) {
      alert("done");
    } else {
      alert("blah");
    }
  },
  "json"
);

And the response script on the server is:

$return['plete'] = 'plete';
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.plete 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):

{"plete":"plete"}

Any pointers on what I might've missed...

I am working on a localhost server - apache on ubuntu - if that makes a difference?

Share Improve this question edited Dec 30, 2009 at 10:54 HorusKol asked Dec 30, 2009 at 10:20 HorusKolHorusKol 8,70610 gold badges57 silver badges94 bronze badges 2
  • 1 could you post the exact json string that firebug shows in the response tab of the xhr request? – David Hedlund Commented Dec 30, 2009 at 10:21
  • have added the string as displayed in the FireBug console – HorusKol Commented Dec 30, 2009 at 10:55
Add a ment  | 

4 Answers 4

Reset to default 5

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.plete);
    if (data.plete) {
      alert("done");
    } else {
      alert("blah");
    }
  },
  "json"
);

I'm not sure how jQuery parses JSON data, but it may be the JSON string is being evaluated incorrectly. If you enter {"plete":"plete"} in the FireBug console, it is interpreted as a block statement rather than an object literal (the "plete" property name is evaluated as a label). This is fixed by evaluating either ({"plete":"plete"}) or {plete:"plete"}, or using JSON.parse on {"plete":"plete"}. The quickest way to fix this would be to remove the "json" parameter from the $.post call and parse the data your self, like so:

$.post("/gallery/resize",
  function (data) {
    var obj;
    if (JSON.parse) {
      obj = JSON.parse(data);
    } else {
      obj = eval("(" + data + ")"); // obligatory eval() warning
    }
    if (data.plete) {
      alert("done");
    } else {
      alert("blah");
    }
  }
);

Incidentally, if you're debugging with Firebug, always try to use console.log instead of alert - it doesn't interrupt execution of the JS, and it gives much more informative JSON serialization than [object Object]

Try this:

header('Content-Type: application/json');
$return = array('plete' => 'plete');
echo json_encode($return);
exit;

JSON data is like a javascript array. So you should access it the same.

Therefore, in the code you provided, you should access the entry "plete" this way instead:

data.items[0].plete

That should do the trick.

You may look here for further jQuery/JSON information.

发布评论

评论列表(0)

  1. 暂无评论