The Ajax of JavaScript

Source: Internet
Author: User

Ajax can be loaded asynchronously, interacting with the server and updating one part of the page without refreshing the entire page. There are two main ways to request it: Get and post.

Because XMLHttpRequest is supported by more than IE6, it is best to declare the object as follows in this way:
if (window. XMLHttpRequest) { var xhr = new XMLHttpRequest ();} else{ var xhr = new ActiveXObject (' Microsoft.XMLHTTP ');}
To connect to the server after creating the object:
Xhr.open (' GET ', ' Server-side code ' address? keyword=c&user_name=mike&age=20 ', true);

Then send the request:
Xhr.send ();

Then accept the return value:
Xhr.onreadystatechange=function () {    if (xhr.readystate==4) {        if (xhr.status==200) {            alert ( Xhr.responsetext);        } else{            alert ("Failed")}}    ;

If you use the Set method then is the above notation, the passed parameters are placed in the URL, multiple parameters with & connection.
If this is the Post method:
Xhr.open ("POST", "ajax.php",true);            Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");            Xhr.send ("key1=x&key2=q&key3=w");            Xhr.onreadystatechange=function() {                if(xhr.readystate==4&& xhr.status==200) {                    alert (xhr.responsetext);                }            }

Above the service side of the code in ajax.php, with the Post method needs to add setRequestHeader method, the wording is shown above. The parameters to be passed are placed inside the Send method.

Get method: Fast, pass the data size is only 4KB, suitable for the transfer of small amount of data, the passed parameters will be exposed in the URL.

Post method: Almost unlimited capacity, in most cases for uploading data.

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