javascript - 如何取消ajax加载

标签 javascript ajax forms xmlhttprequest loading

我有一个加载外部内容的脚本:

<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加载。我想说的是,一旦选择了“选择一个...”选项,如何恢复默认内容。

最佳答案

我认为这样的东西就是你想要的:

function get(obj) {
  var selObj = document.getElementById('port_post');
  var selectedVal = selObj.options[selObj.selectedIndex].value; //this is more Xbrowser compatible than your previous method
  if (selectedVal == 1){
      if (http_request)
         http_request.abort();
      document.getElementById('opciones').innerHTML = 'Default content';
  } else {
      var poststr = "port_post=" + encodeURI( selectedVal );
      makePOSTRequest('http://www.site.com/inc/metaform.php?opcion='+ encodeURI(selectedVal ), poststr);
  }
}

关于javascript - 如何取消ajax加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959943/

相关文章:

javascript - 如何在 JavaScript 中附加窗口调整大小事件监听器?

ajax - 基于选定下拉列表的 django 和 ajax 下拉列表

javascript - 闭包内的 jQuery 变量可见性

google-chrome - Chrome扩展弹出窗口中的表单发布没有做任何事情

javascript - 表单字段验证问题

javascript - cordova 中 fileSystem.root.getFile 的错误代码

javascript - 使用 Apollo Graphql Server 解析关系文档

javascript - 使用 $window 配置 AngularJS 路由到深层路径

jquery - 使用 content_for 和 ajax/Jquery 部分更新网页

angularjs - 在 AngularJS 中,当同一表单中的另一个值发生更改时,如何强制重新验证表单中的字段?