繁体   English   中英

Ajax中的异常处理

[英]Exception handling in Ajax

我正在从HTTPS页面进行以下ajax调用,

 $.ajax({
    url: "http://www.w3schools.com",
    dataType: 'jsonp',
    crossDomain: true,
    complete: function (e, xhr, settings) {
        switch (e.status) {
            case 200:
                //everything fine
                break;
            default: 
                //Somethings wrong, show error msg                                   
                break;
        }
    }
});

但是由于我是从HTTPS页面发出HTTP调用,因此该调用被阻止了。 我可以在控制台(CHROME)中看到“内容不安全警告”,

但是无论是完整,错误还是成功,都不会捕获错误。 他们根本不开火。

我需要抓住错误。 我有什么办法可以做到这一点?

 error
    Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

A function to be called if the request fails. The function receives three arguments: The 

jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that 

occurred and an optional exception object, if one occurred. Possible values for the second 

argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP 

error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not 

Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array 

of functions. Each function will be called in turn. Note: This handler is not called for 

cross-domain script and cross-domain JSONP requests. This is an [Ajax Event][1].

尝试这个,

使用jqXHR.status查找请求的状态

演示

$.ajax({
    url: "http://www.w3schools.com",
    dataType: 'jsonp',
    crossDomain: true,
    complete: function (e, xhr, settings) {
        switch (e.status) {
            case 200:
                //everything fine
                break;
            default: 
                //Somethings wrong, show error msg                                   
                break;
        }
    },
    error: function(jqXHR, textStatus, errorThrown){

    alert(errorThrown)
    },
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM