Home > Web Front-end > JS Tutorial > How to terminate the ajax request being sent in js and jQuery

How to terminate the ajax request being sent in js and jQuery

亚连
Release: 2018-05-24 14:12:43
Original
2250 people have browsed it

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(){
  }
})
Copy after login


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:&#39;POST&#39;,
  url:&#39;b.php&#39;,
  data:&#39;&#39;,
  success:function(){
    alert(&#39;ok&#39;);
  }
})
alert(xhr);
console.log(xhr);
</script>
<button id="song">abort</button>
<script>
$(function(){
  $("#song").click(function(){
    alert(&#39;click&#39;);
    xhr.abort();
  })
})
</script>
Copy after login


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(&#39;响应超时&#39;);
    //关闭请求
    xmlHttp.close();
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template