我的服务器上有处理AJAX调用并返回JSON对象的php,如下所示:
$dataArray = array('order_id'=>$order_id, 'response'=>'Sucessfully Added');
header('Content-type: application/json');
echo json_encode( $dataArray );
这是我的AJAX呼叫:
$('.add').ajaxForm({url: this.href, type:'post',
data: this.serialize,
dataType: 'json',
success: function(responseText){
var p = JSON.parse(responseText);
alert(p.response);
$('.popupContainer').hide();
}
});
在FireBug中,我可以看到它到达了以'var p'开头的行。事实上,在这一点上,FB告诉我,responseText的正是我希望它是:{"order_id":"182","response":"Sucessfully Added"}
。但是到那时它突然停止了,所以我在这里一定错过了一些东西。
无需进行手动解析,因为将dataType
设置为json
,系统会自动解析响应
$('.add').ajaxForm({
url: this.href,
type: 'post',
data: this.serialize,
dataType: 'json',
success: function (p) {
alert(p.response);
$('.popupContainer').hide();
}
});
数据类型
'json':如果dataType =='json',则将评估服务器响应并将其传递给'success'回调(如果已指定)
成功
responseText或responseXML值(取决于dataType选项的值)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句