1

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 backend:

$.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?

3
  • Are we sure it's being evaluated as a JSON response and not plain text? What happens if you use the getJSON call instead or you eval (for testing) the results? Commented Feb 9, 2010 at 2:41
  • 1
    Have you tried specifying dataType: "json" as an option to .ajax() yet? Commented Feb 9, 2010 at 2:41
  • I tried eval(data) before, yes... That returns [object Object],[object Object]. ---- So weird, when I try specifying the dataType it returns the same, even without eval(). What does this mean? Commented Feb 9, 2010 at 2:50

1 Answer 1

3

set dataType

$.ajax({
    type: "POST",
    url: "KMS-backend.php",
    data: "&checkdivpage="+pagename ,
    dataType: 'json',
    success: function(data) {
        alert(data[0].divid);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

My GOD, that did it!!! At last! Thank you so much for the feedback guys, all 3 of your guesses were spot-on. You just saved me a headache, and with that, probably my computer and window glass :) THANK YOU!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.