【问题标题】:AJAX: only error callback is been firedAJAX:仅触发错误回调
【发布时间】:2015-11-12 18:15:16
【问题描述】:

我已经声明了successerror 回调,但在状态码200 的情况下,它也只调用错误回调。

我一直在对registry.php 中的其他一些php 文件进行 curl 调用。

这是我尝试过的:

$.ajax({
  type: "POST"
  ,data: {
    action: "blah"
    ,mobileNo: that.mobNo
    ,token: that.key
  }
  ,url: "http://90.8.41.232/localhost/registry.php"
  ,cache: false
  ,dataType: "json"
  ,success: function (response) {
    console.log("success");
  }
  ,error: function () {
    console.log("error");
  }
});

我在文档中读到我们不必显式调用成功回调,希望它是正确的。

知道如何在状态码为 200 时调用成功回调。

回应 希望这会有所帮助,这是我从 chrome 控制台复制的,不是由 console.log() 打印的。

bort: (statusText)
always: ()
complete: ()
done: ()
error: ()
fail: ()
getAllResponseHeaders: ()
getResponseHeader: (key)
overrideMimeType: (type)
pipe: ()
progress: ()
promise: (obj)
readyState: 4
responseText: "{"success":"Mobile No 9535746622 is approved"}      {"report":true}"
setRequestHeader: (name,value)
state: ()
status: 200
statusCode: (map)
statusText: "OK"
success: ()
then: ()

【问题讨论】:

  • 您的代码看起来正确,来自服务器的答案是有效的 json 吗?
  • 你确定http://90.8.41.232/localhost/registry.php不是跨域数据访问吗?如果是,那么您可能需要通过提供特定标头在 registry.php 中启用 cors,并且您还需要检查 jsonp 以进行跨域数据访问。
  • 错误函数接受三个参数。看看他们,他们可能会告诉你哪里出了问题。 JavaScript 错误控制台也可能会显示错误。您的开发者工具的 Net 选项卡会让您确认响应中是否有 200 OK 状态码,您检查了吗?
  • 是的,我已经检查了所有这些东西@Quentin
  • @PJ1405 — 错误函数的参数说明了什么?鉴于您的编辑,我希望它会告诉您 JSON 解析错误。

标签: javascript php jquery ajax curl


【解决方案1】:

正如您所提到的,您正在向其他一些php 进行curl 调用,当您进行curl 调用时,您必须将curl 的响应返回给客户端。所以要转移curl调用的return值,你必须设置一个选项curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);。 这将解决您的问题。希望如此。

【讨论】:

    【解决方案2】:

    首先,检查你得到的真正错误(不确定你真的得到 200 状态?)

    error: function (err) {
      console.error("error");
      console.log(err);
      console.log(err.stack);
      throw err;
    }
    

    请向我们展示生成的日志。

    【讨论】:

    • 我添加了响应对象,状态码200 n ok
    • 请把console.log(err);就像你会看到抛出的错误细节
    【解决方案3】:

    几周前我就这样做了,对我来说,如果我这样称呼它就可以了

    $.ajax(
    {
        type: "POST",
        data: { action: "blah", mobileNo: that.mobNo, token: that.key },
        url: "http://90.8.41.232/localhost/registry.php",
        cache: false,
        dataType: json,
    })
    .success (function (data, status, jqxhr)
    {
        // do your stuff
    })
    .error (function(x, status, jqxhr) 
    {
        // handle your errors
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多