The Ajax use of JavaScript

Source: Internet
Author: User

using Ajax to define a XMLHttpRequest variable first, in order to determine compatibility with IE6 and the following versions, you need to
var xmlhr;
if (window. XMLHttpRequest) {
XMLHR = new XMLHttpRequest ();
}else{
XMLHR = new ActiveXObject ("Microsoft.XMLHTTP");
        }
Common Properties and methods for XMLHttpRequest objects are:
three common properties of the XMLHttpRequest object:

1. onreadystatechange Properties
the onReadyStateChange property has functions that handle server responses.

The following code defines an empty function that can be set at the same time as the onReadyStateChange property:

xmlhr.onreadystatechange=function ()
  {
//We need to write some code here
  }
2.readyState Properties
The ReadyState property holds state information about the server response. The onreadystatechange function is executed whenever readyState changes.

possible values for the ReadyState property:

Status Description
0 The request was not initialized (before calling open ())
1 request has been made (before calling Send ())
2 The request has been sent (here you can usually get the content header from the response)
3 in Request processing (usually some of the data in the response is available, but the server has not yet completed the response)
4 request completed (Can access server response and use it)

we want to add an if statement to this onreadystatechange function to test whether our response is complete (which means we can get the data):

xmlhr.onreadystatechange=function ()
  {
if (xmlhttp.readystate==4)
    {
//Get data from the server's response
    }
  }
3.responsetext Properties
the data returned by the server can be retrieved through the ResponseText property.

in our code, we will set the value of the time text box to equal to ResponseText:

xmlhr.onreadystatechange=function ()
  {
if (xmlhr.readystate==4)
    {
Document.myform.time.value=xmlhttp.responsetext;
    }
  }

Methods of Xmlhttprequst

1.open () method

Open () has three parameters. The first parameter defines the method used to send the request, the second parameter specifies the URL of the server-side script, and the third parameter specifies that the request should be processed asynchronously.

Xmlhr.open ("GET", "test.php", true);

2.send () method

the Send () method will send the request to the server. If we assume that the HTML file and the PHP file are in the same directory, then the code is:

xmlhr.send (null);



XMLHttpRequest Ajax uses the general steps
Open ("GET" or "POST") setRequestHeader ("Content-type", "application/x-www-form-encoded") Send Onreadystat Echange

The Ajax use of 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.