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

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

问题描述

我在做一个ajax JSONP请求,但失败的错误处理不会工作。如果请求是404或500,将无法处理错误。

我一直在四处寻找找到一个答案,但无法找到任何东西。似乎有与 HTTP的解决方案://$c$c.google.com/p/jquery- JSONP / ,但我无法找到如何使用它的任何实例。

 功能验证(user和pass){
    $阿贾克斯({
        键入:POST,
        网址:URL,
        数据类型:JSONP,
        异步:假的,
        发送到验证URL // json对象
        数据:{う:用户ID,p的:通},

        成功:功能(数据){
            //验证成功在这里
            的console.log(数据);
        },
        错误:函数(XHR,textStatus,errorThrown){
            警报(错误:+ textStatus);
            警报(错误:+ errorThrown);
        }
    })
}
 

解决方案

两种方式来处理错误,

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

  2. jQuery的AJAX超时 - 超时后的时间来触发错误回调,因为它可能会默默地失败的合理费用。你可能不知道什么是真正的错误(或错误状态)是,但至少你得处理错误

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.

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

解决方案

Two ways to handle error,

  1. There is no error handling for cross domain JSONP requests. Use jsonp plug-in available on Github https://github.com/jaubourg/jquery-jsonp that provides support for error handling.

  2. 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天全站免登陆