重新调用有效JSON时调用的Ajax错误回调

时间:2015-08-09 12:37:32

标签: javascript php ajax json

在我的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无效,就会发生这种情况,但我觉得不是。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:6)

我认为问题出在您的dataType属性上。您已将其设置为"application/json"。虽然这是值mimeType值,但$.ajax函数需要以下预定义值之一:the relevant page of the documentation中列出的xml, json, script, or html

尝试拨打$.ajax看起来像这样:

$.ajax({
      type: "POST",
      dataType: "json",
      url: "php/parseFunctions.php",
      data: {data:queryObj},
      success:{...},
      complete:{...},
      error:{...}
});