在我的js文件中,我调用以下代码:
$.ajax({
type: "POST",
dataType: "application/json",
url: "php/parseFunctions.php",
data: {data:queryObj},
success: function(response) {
theFunction(response);
},
complete: function(response) {
theFunction(response);
},
error: function(response) {
theFunction(response); // response = Object {readyState: 4, responseText: "{"found":0}", status: 200, statusText: "OK"
}
});
在我的php/parseFunctions.php
我有:
$returnResults = array();
$returnResults['found']=count($returnResults);
echo json_encode($returnResults);
exit;
我希望success
回调被调用,response
是json对象{"found":"0"}
error
回调被调用,response = Object {readyState: 4, responseText: "{"found":"0"}", status: 200, statusText: "OK"}
我读到,如果返回的JSON是无效的,这将发生,但我不觉得它是。
我在这里做错了什么?
我认为问题在于您的dataType
属性。您已将其设置为"application/json"
。虽然这是一个mimeType值,但$.ajax
函数期望下列预定义值之一:xml, json, script, or html
,如文档的相关页面所列。
试着让你对$.ajax
的调用看起来像这样:
$.ajax({
type: "POST",
dataType: "json",
url: "php/parseFunctions.php",
data: {data:queryObj},
success:{...},
complete:{...},
error:{...}
});