Js and jQuery terminate the ajax request being sent,
This article describes how js and jQuery terminate the ajax request being sent. We will share this with you 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. ($. Post, $. ajax, $. getJSON, and $. getScript are the same)
2. the XMLHttpRequest object has the abort () method.
You can also manually call the abort method:
<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 (){... // operation after response} xmlHttp. send (); // after 8 seconds, check whether the data sent by the xmlHttp object has received a response. setTimeout ("CheckRequest ()", "8000"); function CheckRequest () {// The if (xmlHttp. readyState! = 4) {alert ('response timeout'); // close the request xmlHttp. close ();}}
I hope this article will help you with ajax programming.
Articles you may be interested in:
- Compatible with multiple browser Ajax request instances implemented by js and jQuery
- Jquery tutorial ajax request json data example
- Perform a cross-origin ajax request test using $. getJSON of jquery.
- Detailed description of ajax request json data case
- Example of an original Ajax request in html + js + php
- How to implement an ajax request every five minutes in Javascript