在firefox中,使用jquery ajax不会调用错误和完整回调

error and complete callbacks are not called in firefox using jquery ajax

本文关键字:错误 调用 回调 firefox 使用 ajax jquery      更新时间:2023-09-26

以下操作符合预期。

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
            else if (xmlhttp.status == 400) {
                alert('There was an error 400')
            }
            else {
                alert('something else other than 200 was returned:' + xmlhttp.status);
            }
        }
    }
    xmlhttp.open("GET", "/services", true);
    xmlhttp.send();

它提醒"返回了200以外的其他内容:404"。

但是下面的ajax和jquery不起作用。

     $.ajax({
        type: "get",
        url: "/services",    
        error: function() {
            console.log("A");
        },
        complete: function (xhr, data) {
            console.log(arguments);
            if (xhr.status != 0)
                alert('success1');
            else
                alert('fail2');
        }
    })

Firebug显示404,但没有调用上面代码中的警报或控制台。我一直依靠jquery来解决跨浏览器问题,现在我正在考虑回到XMLHttpRequest()。

事实证明,这个问题是由于我的构建脚本损坏了我的代码(包括jquery)。我不知道为什么它之后不起作用。

但是在requirebuildgrunt文件中设置optimize: "none",,使一切正常。