JSONP请求错误处理

阿德里安·莫希卡(Adrian Mojica)

我正在发出ajax jsonp请求,但是失败错误处理无法正常工作。如果请求为404或500,则不会处理该错误。

我一直在四处寻找答案,但是什么也找不到。http://code.google.com/p/jquery-jsonp/似乎有一种解决方案,但是我找不到任何有关如何使用它的示例。

function authenticate(user, pass) {       
    $.ajax ({
        type: "POST",
        url: "url",
        dataType: 'jsonp',
        async: false,
        //json object to sent to the authentication url
        data: {"u": userid, "p": pass},

        success: function (data) {
            //successful authentication here
            console.log(data);
        },
        error: function(XHR, textStatus, errorThrown) {
            alert("error: " + textStatus);
            alert("error: " + errorThrown);
        }
    })
}
shyammtp

处理错误的两种方法

  1. 跨域JSONP请求没有错误处理。使用Github上提供的jsonp插件https://github.com/jaubourg/jquery-jsonp,它提供了对错误处理的支持。

  2. jQuery ajax超时-在合理的时间后触发错误回调的超时,因为它可能会静默失败。您可能不知道实际的错误(或错误状态)是什么,但至少可以处理该错误

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章