Help scripts for asynchronous objects (XMLHttpRequest)

Source: Internet
Author: User

Asynchronous Object Five Section

This is the POST request,

 //1.00 Creating an Asynchronous Object            varXHR =NewXMLHttpRequest (); //2.0Xhr.open ("Post", Url,params,true); //3.0 passing parameters using the Formdata propertyXhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); //4.0 Setting the callback functionXhr.onreadystatechange =function () {                if(Xhr.readystate = = 4 && xhr.status = 200) {alert (xhr.responsetext); }            }            //5.0 Passing ParametersXhr.send (params);

Encapsulates an asynchronous object with a GET request

In the GET request

  Xhr.setrequestheader ("If-modified-since", "0"); is to clear the cache

While the POST request
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

It's for transmission.
At <from method= ' post ' type= "" >
The type in <from> can be obtained in three ways, including application/x-www-form-urlencoded

varAjaxhelp ={creatxhr:function () {        //Creating an Asynchronous object        varXHR =NewXMLHttpRequest (); returnXHR; },    //GET requests for AjaxAjaxget:function(URL, callBack) { This. Ajaxcommon ("Get", url,NULL, CallBack); },    //POST request for AjaxAjaxpost:function(URL, params, callBack) { This. Ajaxcommon ("POST", URL, params, callBack); }, Ajaxcommon:function(method, URL, params, callBack) {//1.0        varXHR = This.        CREATXHR (); //2.0Xhr.open (method, URL,true); //3.0        if(method = = "Get") {Xhr.setrequestheader ("If-modified-since", "0"); } Else{Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); }        //4.0Xhr.onreadystatechange =function () {            if(Xhr.readystate = = 4 && xhr.status = 200) {                varDatas =Json.parse (Xhr.responsetext); //Execute callback functionCallBack (datas); }        }        //5.0xhr.send (params); }};

PS: In jquery there are $.ajax and $.get/$. The method of the asynchronous request such as Post. The previous package will not be used. Amount It's a good rip. In fact, they are the bottom of the same writing. jquery is designed to solve the compatibility problems of each browser.

Help scripts for asynchronous objects (XMLHttpRequest)

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.