jQuery ajax jsonp在成功后调用错误

时间:2022-10-10 19:41:07

I'm using jQuery 1.9.1 performing jsonp against the Etsy API. Below is a routine used to call into the API; it accepts a signed request (url), a jsonp callback name and an error handler.

我使用jQuery 1.9.1对Etsy API执行jsonp。下面是一个用于调用API的例程;它接受签名的请求(url)、jsonp回调名和错误处理程序。

call: function (signedOAuthRequest, callback, errorFn) {
    $.ajax({
        url: signedOAuthRequest,
        type: "GET",
        dataType: "jsonp",
        jsonpCallback: callback,
        jsonp: false,
        crossDomain: true,
        cache: true,
        processData: false,
        error: function (jqXHR, textStatus, errorThrown) {
            if (errorFn != undefined) {
                errorFn(jqXHR, textStatus, errorThrown);
            }
        }
    });

Usage:

用法:

call("http://the.api/?signedrequest", "myCallback", myErrorHandler);

And it works. myCallback fires, the json object received looks good. Everything's fine.

和它的工作原理。myCallback触发,收到的json对象看起来不错。一切都很好。

Except...

除了……

Immediately after myCallback completes, the error handler runs. In the error handler, the status code is 200 and the error message is "parsererror". errorThrown.message is "myCallback was not called". Thing is, myCallback was definitely called and the json object passed to it was parsed just fine, so I don't get this at all.

在myCallback完成之后,错误处理程序立即运行。在错误处理程序中,状态码为200,错误消息为“parsererror”。errorThrown。消息是“myCallback未调用”。问题是,myCallback被调用,而传递给它的json对象被解析得很好,所以我根本没有得到这个。

Why is this error being thrown on an otherwise successful call?

为什么这个错误被抛出到另一个成功的调用中?

1 个解决方案

#1


1  

Based on comments and what's in your question, this is how i would write that code:

根据你的评论和你的问题,这就是我写代码的方式:

function (signedOAuthRequest, callbackName, successFn, errorFn) {
    $.ajax({
        url: signedOAuthRequest,
        type: "GET",
        dataType: "jsonp",
        jsonpCallback: callbackName,
        jsonp: false,
        cache: true,
        success: successFn,
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof errorFn !== 'undefined') {
                errorFn(jqXHR, textStatus, errorThrown);
            }
        }
    });
}

I made the following changes:

我做了以下改动:

Added an additional parameter to pass a success callback. This is required because jQuery will define the callback for you so that it can retrieve the successful response and parse it.

添加一个附加参数以传递成功回调。这是必需的,因为jQuery将为您定义回调,以便它能够检索成功的响应并解析它。

Removed crossdomain: true because it is redundant

删除的交叉域:为真,因为它是冗余的

Removed proccessData: false because it doesn't affect jsonp requests and you aren't passing any data

已删除的proccessData: false,因为它不影响jsonp请求,也不传递任何数据

Added success: successFn so that jQuery will execute successFn on success with the parsed json data.

添加成功:successFn使jQuery能够成功地使用解析的json数据执行成功。

Renamed callback to callbackName so that it's more obvious about what it contains.

将回调重命名为callbackName,以便更清楚地了解它包含的内容。

An alternate solution if you still insist on defining the callback yourself is to use the script dataType (taken from comments below)

如果您仍然坚持自己定义回调,另一种解决方案是使用脚本数据类型(取自下面的注释)

$.ajax({
    url: signedOAuthRequest,
    dataType: "script",
    cache: true
});

#1


1  

Based on comments and what's in your question, this is how i would write that code:

根据你的评论和你的问题,这就是我写代码的方式:

function (signedOAuthRequest, callbackName, successFn, errorFn) {
    $.ajax({
        url: signedOAuthRequest,
        type: "GET",
        dataType: "jsonp",
        jsonpCallback: callbackName,
        jsonp: false,
        cache: true,
        success: successFn,
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof errorFn !== 'undefined') {
                errorFn(jqXHR, textStatus, errorThrown);
            }
        }
    });
}

I made the following changes:

我做了以下改动:

Added an additional parameter to pass a success callback. This is required because jQuery will define the callback for you so that it can retrieve the successful response and parse it.

添加一个附加参数以传递成功回调。这是必需的,因为jQuery将为您定义回调,以便它能够检索成功的响应并解析它。

Removed crossdomain: true because it is redundant

删除的交叉域:为真,因为它是冗余的

Removed proccessData: false because it doesn't affect jsonp requests and you aren't passing any data

已删除的proccessData: false,因为它不影响jsonp请求,也不传递任何数据

Added success: successFn so that jQuery will execute successFn on success with the parsed json data.

添加成功:successFn使jQuery能够成功地使用解析的json数据执行成功。

Renamed callback to callbackName so that it's more obvious about what it contains.

将回调重命名为callbackName,以便更清楚地了解它包含的内容。

An alternate solution if you still insist on defining the callback yourself is to use the script dataType (taken from comments below)

如果您仍然坚持自己定义回调,另一种解决方案是使用脚本数据类型(取自下面的注释)

$.ajax({
    url: signedOAuthRequest,
    dataType: "script",
    cache: true
});