Implementing AJAX asynchronous requests

Source: Internet
Author: User

1, remove both ends blank

    function return str.replace (/(^\s*) | ( \s*$)/g, "");}

Implementing Ajax

Ajax:

Asynchronous: Asynchronous

Javascript

And: And

Xml

Ajax technology is the ability to use JavaScript and XML to implement asynchronous interactions

Ajax has its own engine, and the traditional interaction is that the client interacts directly with the sever,

Ajax is the client sends the request to the Ajax engine, and the Ajax engine sends the request to the server,

This implements the asynchronous request

1. Creating an Ajax Object

function createxhr () {    try{returnnew XMLHttpRequest ()}catch(e) {}                      //W3C, also supported    in IE High version Try {returnnew activexobject (' Microsoft.XMLHTTP ')} Catch (e) {}   //ie    alert (' Please change browser ');}

2. Initializing Ajax objects

Xhr.open (' Get ', ' demo01.php ');//open (Mehod,url)

You can also set the request header information here

setRequestHeader (Header,value)///can use 1 (' if-modified-since ', ' 0 ') to eliminate the cache, or you can use the plus random number 2 math.random ()

or random time 3 var url= ' demo01.php?name=huohaifeng&_= ' + new Date (). GetTime ();

or 4 Set the header ("Cache-control:no-cache") on the server side;

3. Send Request

Send (content) only sends the request when the Send method is called, content:post the data that is passed when the request is requested, and if the GET request writes NULL directly

4. Execute callback function

Onreadystatechange=funtion () {} triggers when the Ajax object status code changes

Some other Ajax properties:

Readystate:ajax Object Status Code: 0-4

0: Indicates that the object is established but not initialized

1: Indicates that the object has been initialized but not sent

2: The Send method has been called for request

3: Receiving data

4: Receive complete

Status

The HTTP response status code received by the Ajax object, such as: 200 302 304 404

StatusText

The HTTP response status text received by the Ajax object such as: OK, not found

Reponsetext

The Ajax object receives the HTTP response body string (text/html)

Responsexml

HTTP response body content received by the Ajax object (Text/xml)

Implementing AJAX asynchronous requests

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.