我有一个点击链接,其中的一个请求应该去web服务器,并在成功执行时,应该会发生重定向。我已经使用了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;
}重定向正在发生,但我在火狐中得到了NS_Binding_Aborted错误。在成功和错误的情况下,重定向都应该发生,但为什么NS_Binding_Aborted会出现,我不确定这一点。只有当一个事件取消一些先前运行的事件时,才会出现NS_Binding_Aborted错误,但我已经抑制了链接的href,并在ajax请求执行后将其重定向,因此应该只有一个服务器调用,而NS_Binding_Aborted不应该出现。请让我知道我哪里错了?
发布于 2013-05-28 03:21:00
这是由中止您的请求的另一个请求引起的。通常,当你的目标是重新加载所有页面的数据时,只是结束请求,而不是同步请求,这会有一点novell错误。
在这种情况下,"return“语句就是问题所在,return语句必须在success seccion中。
发布于 2021-03-03 18:02:28
在onclick中同时使用href和XmlHttpRequest时,我也遇到了类似的问题。我的XMLHttpRequest已中止(ns_binding_aborted),因此从未达到状态200。我还可以在Firefox控制台中看到我的XHR被“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能够成功完成。
https://stackoverflow.com/questions/13085158
复制相似问题