无法在JQuery $ .get()中返回/处理JSON [英] Unable to return/process JSON in JQuery $.get()
本文介绍了无法在JQuery $ .get()中返回/处理JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
调用$ .get()函数时,返回/处理JSON数据时出现问题.这是代码:
jQuery:
I have a problem returning/processing JSON data while calling $.get() function. Here's the code:
JQuery:
$.get("script.php?",
function(data) {
if (data.status) {
alert('ok');
} else {
alert(data.error);
}
},'json');
PHP
if ($r) {
$ret = array("status"=>true);
} else {
$ret = array("status"=>false,"error"=>$error);
}
echo json_encode( $ret );
这是代码.但是,响应始终在jquery中被视为字符串. data.status和data.error为undefined
.
So this is the code. But the response is always taken as string in the jquery. data.status and data.error is undefined
.
推荐答案
您需要使用jQuery 1.4.x,否则必须自己解析返回的数据.
You need to use jQuery 1.4.x, otherwise you have to parse the returned data by yourself.
data = JSON.parse(data);
这篇关于无法在JQuery $ .get()中返回/处理JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文