up vote 0 down vote favorite

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?

flag
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? – Jonathon Feb 9 at 2:41
1  
Have you tried specifying dataType: "json" as an option to .ajax() yet? – Crescent Fresh Feb 9 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? – Adrian van Vliet Feb 9 at 2:50

1 Answer

up vote 2 down vote accepted

set dataType

$.ajax({
    type: "POST",
    url: "KMS-backend.php",
    data: "&checkdivpage="+pagename ,
    dataType: 'json',
    success: function(data) {
        alert(data[0].divid);
    }
link|flag
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! – Adrian van Vliet Feb 9 at 2:55
You're welcome :) – Luke Schafer Feb 11 at 1:23

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.