1

i have an array in php like this:

$arr['test'] = "asd";
$arr['test1'] = "asd1";
echo json_encode($arr);

and i get this using jquery post:

$.post("fillScheda.php",{scheda:num_scheda,id:idCliente},function(msg){

});

so in msg there is my json array... i tried to access in some way..

alert(msg[0].test);

alert(msg.test);

but result is always undefined... how can i access to values? can someone help me? thanks!!!

3
  • msg.test should work. Can you double check your server is outputting {"test":"asd","test1":"asd1"}, and nothing more via the network tab in your developer tools? Commented Jun 20, 2012 at 10:29
  • msg = eval('(' + msg + ')'); alert(msg.test); Commented Jun 20, 2012 at 10:30
  • @Matt i test using alert(msg) that the string resulted is the same you post.. Commented Jun 20, 2012 at 10:38

3 Answers 3

3

Try to parse the json object

msg = JSON.parse(msg);
Sign up to request clarification or add additional context in comments.

Comments

0

have you tried to set the type of return value of json:

$.post("fillScheda.php",{scheda:num_scheda,id:idCliente},function(msg){

},'json');

Comments

0

You should serve json from the php file with this header:

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

So the code should be:

$arr['test'] = "asd";
$arr['test1'] = "asd1";

header('Content-type: application/json');
echo json_encode($arr);
exit();

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.