This article mainly introduces the method of js and jQuery to terminate the ajax request being sent. It analyzes the implementation techniques and related precautions of jQuery and JavaScript to terminate the ajax request with examples. It has certain reference value. Friends in need can refer to it.
The example in this article describes the method of js and jQuery to terminate the ajax request being sent. Share it with everyone for your reference, the details are as follows:
Core: Call the abort method on the XMLHttpRequest object
jquery's ajax method has its own timeout setting parameters:
$.ajax({type:'POST', url:'b.php', data:'', timeout:5000, success:function(){ } })
At the same time
1. The data type returned by $.get is XMLHttpRequest, please refer to the manual. (The same applies to $.post, $.ajax, $.getJSON, and $.getScript)
2. The XMLHttpRequest object has abort() method
You can also call the abort method manually:
<script src = "jquery-1.4.4.js"></script> <script> var xhr = $.ajax({type:'POST', url:'b.php', data:'', success:function(){ alert('ok'); } }) alert(xhr); console.log(xhr); </script> <button id="song">abort</button> <script> $(function(){ $("#song").click(function(){ alert('click'); xhr.abort(); }) }) </script>
For native xhr:
xmlHttp.open("POST","theUrl",true); xmlHttp.onreadystatechange=function(){ ...//得到响应之后的操作 } xmlHttp.send(); //设置8秒钟后检查xmlHttp对象所发送的数据是否得到响应. setTimeout("CheckRequest()","8000"); function CheckRequest(){ //为4时代表请求完成了 if(xmlHttp.readyState!=4){ alert('响应超时'); //关闭请求 xmlHttp.close(); } }
The above is what I compiled Everyone, I hope it will be helpful to everyone in the future.
Related articles:
Django framework uses ajax to implement batch import data function
AJAX XMLHttpRequest object detailed explanation
The third implementation of writing lightweight ajax components
##
The above is the detailed content of How to terminate the ajax request being sent in js and jQuery. For more information, please follow other related articles on the PHP Chinese website!