1

i have this php code:

$query = mysql_query("SELECT * FROM table WHERE id=$id");
while($item = mysql_fetch_assoc($query)){
    $dataArray['var1'] = $item['var1'];
    $dataArray['var2'] = $item['var2'];
    $dataArray['var3'] = $item['var3'];
    array_push($return_arr,$dataArray);
}

echo json_encode($return_arr);

it's in my ajax.php file

Then I'm using jquery to send there id via ajax request:

$.ajax({
    type: "POST",
    url: 'ajax.php',
    data: 'id='+id,
    success: function(data) { 
        alert(data);
    }
 });

and it alerts me this: [{"var1":"somevarhere","var2":"somevarhere2","var3":"somevarhere3"}]

can someone tell me please how I can access for ex. var3 to get somevarhere3?

thanks

1
  • 1
    So your question is how to transform that json encoded string to an object? Commented Mar 27, 2011 at 19:47

3 Answers 3

4

According to the documentation of jQuery.ajax, the data it receives as first parameter is interpreted depending on the value of the dataType option passed to jQuery.Ajax.

What if you pass 'json' for that parameter ?
Like this :

$.ajax({
    type: "POST",
    url: 'ajax.php',
    data: 'id='+id,
    dataType: 'json', 
    success: function(data) { 
        alert(data);
    }
 });

Doing this, jQuery.ajax should interpret the data returned from the server as JSON.


Else, the documentation says that, if no dataType is specified (quoting) :

jQuery will try to infer it based on the MIME type of the response

So, the other solution would be to return some JSON content-type from the PHP code (see The right JSON content type?), with something like this :

header('Content-type: application/json');

Before doing the echoing.

And, again, jQuery.ajax should interpret the data returned from the server as JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

I have added dataType: 'json', - this looks almost great, now it says that data is Object but when I alert(data.toSource()) it still returns [{"var1":"somevarhere","var2":"somevarhere2","var3":"somevarhere3"}] and therefore it doesnt work. It think that it's because of those brackets ([])
well, now that your data is interpreted as JSON, you can access it like any other Javascript array that would contain one object ; for example : data[0].var3
1

Ok, I found the solution. Thanks to Pascal MARTIN I got the idea to parse the result so I used this:

data = data.substring(1,data.length-1);
data = $.parseJSON(data);
alert(data.var3);

first line removes [], second line parses JSON to Object, and third line accesses the property

1 Comment

Well, if your SQL query always return one, and only one, result, you'd better return a single object from PHP, and not an array that contains one object -- which is why those [] are there : your PHP code returns a JSON-encoded array.
0

Should be able to access it like this:

data[0].var3

Where 0 is the index of the item in the array you want to access.

Comments

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.