开发者

Utilising JSON values in Javascript

开发者 https://www.devze.com 2022-12-20 01:41 出处:网络
I\'m originally a PHP programmer and have been struggling with this for at least 2 whole 9-to-5 days now. I\'ve come a long way but I seem to have gotten stuck trying to figure out the last bit. It SH

I'm originally a PHP programmer and have been struggling with this for at least 2 whole 9-to-5 days now. I've come a long way but I seem to have gotten stuck trying to figure out the last bit. It SHOULD be fairly simple, but somehow I can't seem to find anything that coulp help me figure it out.

I have the folliwing jQuery code that returns some values from the PHP bac开发者_开发技巧kend:

$.ajax({
  type: "POST",
  url: "KMS-backend.php",
  data: "&checkdivpage=" + pagename,
  success: function(data) {
    alert(data);
  }
})

This successfully alerts the returned JSON data:

[{
  "divid": "col-whole"
}, {
  "divid": "col-halfleft"
}]

...Now what I can't seem to figure out, is how to turn this JSON object into an array, so I can loop the returned values! I can't even figure out how to return the first value seperately. Every answer I can find explains you can return each individual result with data[0], data[1], data[2] etc, like with a normal array, but this just returns the character in that position!

How can I return these values so that I can loop each of them seperately?


set dataType

$.ajax({
    type: "POST",
    url: "KMS-backend.php",
    data: "&checkdivpage="+pagename ,
    dataType: 'json',
    success: function(data) {
        alert(data[0].divid);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消