JSONP 请求错误处理 [英] JSONP request error handling

查看:43
本文介绍了JSONP 请求错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发出一个 ajax jsonp 请求,但失败错误处理不起作用.如果请求是 404 或 500,它不会处理错误.

I'm making an ajax jsonp request, but the failure error handling wont work. If the request is 404 or 500 it won't handle the error.

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

I've been looking around to find an answer to this, but can't find anything. There seems to be a solution with http://code.google.com/p/jquery-jsonp/, but I can't find any examples on how to use it.

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);
        }
    })
}

推荐答案

两种处理错误的方法,

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

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

jQuery ajax Timeout - Timeout after a reasonable amount of time to fire the error callback because it might have failed silently. You may not know what the actual error (or error status) was but at least you get to handle the error

这篇关于JSONP 请求错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆