如何取消ajax加载
我有一个加载外部内容的脚本:
<script type="text/javascript">
var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
document.getElementById('opciones').innerHTML = result;
} else {
alert('Hubo un problema con la operación.');
}
}
}
function get(obj) {
var poststr = "port_post=" + encodeURI( document.getElementById("port-post").value );
makePOSTRequest('http://www.site.com/inc/metaform.php?opcion='+ encodeURI( document.getElementById("port-post").value ), poststr);
}
</script>
这是检索内容的选择:
<select name="port_post" id="port-post" onchange="get(this.parentNode);">
<option value="1">Select one...</option>
<option value="2">Pear</option>
<option value="3">Pineapple</option>
</select>
这是容器 div:
<div id="opciones">Default content</div>
我想知道的是,当我将选择更改为“选择一个...”时,如何取消 ajax 加载设置”。我想说的是,一旦选择“选择一个...”选项,如何恢复默认内容。
I have this script which loads external content:
<script type="text/javascript">
var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
document.getElementById('opciones').innerHTML = result;
} else {
alert('Hubo un problema con la operación.');
}
}
}
function get(obj) {
var poststr = "port_post=" + encodeURI( document.getElementById("port-post").value );
makePOSTRequest('http://www.site.com/inc/metaform.php?opcion='+ encodeURI( document.getElementById("port-post").value ), poststr);
}
</script>
This is the select that retrieves the content:
<select name="port_post" id="port-post" onchange="get(this.parentNode);">
<option value="1">Select one...</option>
<option value="2">Pear</option>
<option value="3">Pineapple</option>
</select>
And this is the container div:
<div id="opciones">Default content</div>
All I whish to know is how I can unset the ajax loading when I change the selection to "Select one...". I wish to say, how restoring the Default content once the "Select one..." option is selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这样的事情就是你想要的:
I think something like this would be what you want:
如果每次都使用相同的 XMLHttpRequest 对象,而不是在每次调用时创建一个新实例,则挂起的连接将被中止,事件处理程序将被断开,并且在调用 http_request.open()< 时该对象将被重置/code> 第二次(或第三次等)时间 (参考)。
If you use the same XMLHttpRequest object every time, instead of making a new instance at every call, the pending connection will be aborted, the event handler will be disconnected and the object will be reset when calling
http_request.open()
a second (or third, etc.) time (reference).