调用Json数据将返回未定义的“
问题描述:
当我从php文件调用json对象时,它返回未定义.我可以看到所有写入alert(data)
的数据,但是当我编写alert(data.books)时,它返回未定义的值.
When I call json object from php file it returns undefined. I can see all data writing alert(data)
but when I write alert(data.books) it returns undifined.
$JSON = '
{
"books": {
"book1": "firstbook",
"book2": "secondbook"
}
}
';
我用jQuery来称呼
and I call it with jquery
jQuery('#login').live('submit',function(event) {
$.ajax({
url: 'lib/login.php',
type: 'POST',
dataType: 'json',
data: $('#login').serialize(),
success: function(data ) {
alert(' ' +data.books);
if(data.books.book1){
alert("OK");
}else
{
alert("error");
}
}
});
return false;
});
编辑 这是它返回alert(data)的方式
EDIT This is how it returns alert(data)
{
"books": {
"book1": "firstbook",
"book2": "secondbook"
}
}
答
我在.php文件中更改了
I changed in .php file like
$arr = array ( "book1" => "firstbook" ,"book2" => "secondbook" );
现在它显示当我写警报(data.book1)放出fisrtbook时,用于检查(data.book1)是否有效.
now it shows when i write alert(data.book1) out put fisrtbook .for checking if(data.book1) it works.