Js and jQuery terminate the ajax request being sent,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.