【问题标题】:NS_Binding_Aborted error for ajax functionajax 函数的 NS_Binding_Aborted 错误
【发布时间】:2012-10-26 10:27:03
【问题描述】:

我有一个点击链接,请求应该发送到网络服务器,成功执行后应该会发生重定向。我为此使用了 ajax,但在 HTTpFox 中出现 NS_Binding_Aborted 错误。 代码:

<a id="lnkredirect" href="javascript:void(0);" onclick="myfunction();">Some text</a>

ajax 代码:

function myfunction(){
 $.ajax({
       url: Web server Url,
       type: 'POST',
       datatype: 'JSON',
       timeout: 20000,
       data: null,
       success: function{ $("#lnkredirect").attr('href','redirection link...');},
       error : function{ $("#lnkredirect").attr('href','redirection link...');}
 )};
 return true;
}

重定向正在发生,但我在 Firefox 中收到 NS_Binding_Aborted 错误。在成功和错误的情况下,重定向都应该发生,但为什么 NS_Binding_Aborted 会到来,我不确定这一点。 NS_Binding_Aborted 错误应该仅在一个事件取消某个先前运行的事件时出现,但我已经抑制了链接的 href 并在执行 ajax 请求后重定向它,因此应该只有一个服务器调用并且 NS_Binding_Aborted 不应该出现。请让我知道我哪里出错了?

【问题讨论】:

    标签: ajax firefox jquery httpfox


    【解决方案1】:

    在 onclick 中同时使用 href 和 XmlHttpRequest 时,我也遇到了类似的问题。我的 XMLHttpRequest 被中止(ns_binding_aborted),因此从未达到状态 200。我还可以看到我的 XHR 在 Firefox 控制台中“被 devtools 阻止”。

    这是因为页面在完成其工作(onclick 中的内容)之前(通过 href)重新加载。

    我有这样的事情:

    <script type="text/javascript">
    function incrementNumberOfDownloads() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) { // 4 = request ended, 200 = success
                //update displayed number of downloads
                document.getElementById("numberOfDownloads").innerHTML = this.responseText; 
            }
        };
        xhttp.open("GET", "incrementNumberOfDownloads.php", true);
        xhttp.send();
        return true;
    }
    </script>
    <p id="numberOfDownloads">42</p>
    <a href="files/myFileToDownload.zip" onclick="return incrementNumberOfDownloads();">Download my file !</a>
    

    我通过在我的下载链接中添加 target="_blank" 解决了这个问题,这样在点击时页面不再重新加载,从而使 XMLHttpRequest 能够成功完成。

    【讨论】:

      【解决方案2】:

      这是由另一个中止您的请求的请求引起的。通常,当您的目标是重新加载所有页面的数据时,只需结束请求并且不要同步请求,就会出现一点 Novell 错误。
      在这种情况下“return”语句有问题,return语句必须在success seccion中。

      【讨论】:

        猜你喜欢
        • 2022-08-05
        • 1970-01-01
        • 2011-10-11
        • 2011-01-12
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        相关资源
        最近更新 更多