Native Ajax notation that can be copied directly to apply

Source: Internet
Author: User

Method One:

function Createxmlhttprequest () {//1. Create a XMLHttpRequest object//This is the most complex step in the Xmlhttpreuquest object's use of no part//you need to write different code in different ways for IE and other types of browsers to build this object                varXMLHttpRequest; if(window. XMLHttpRequest) {//for FIREFOX,MOZILLAR,OPERA,SAFARI,IE7,IE8XMLHttpRequest =NewXMLHttpRequest (); //bug fixes for certain specific versions of Mozillar browser                    if(Xmlhttprequest.overridemimetype) {Xmlhttprequest.overridemimetype ("Text/xml"); }                } Else if(window. ActiveXObject) {//for IE6,IE5.5,IE5//Two control names that can be used to create XMLHttpRequest objects, saved in an array of JS//Newer version in front of the line                    varActivexname = ["msxml2.xmlhttp","Microsoft.XMLHTTP"];  for(vari =0; i < activexname.length; i++) {                        Try {                            //remove a control name to create and terminate the loop if the creation succeeds//If the creation fails, the exception is thrown back, then the loop can continue and the attempt to createXMLHttpRequest =NewActiveXObject (Activexname[i]); if(XMLHttpRequest) { Break; }                        } Catch(e) {} }}returnXMLHttpRequest; }//Post Methodfunction post () {//receive with variables after creating an AJAX native environment                varreq =createxmlhttprequest (); if(req) {//method of parameter, URL, whether asynchronous (Asyn)Req.open ("POST","webajax.ashx",true); //post must be set to this typeReq.setrequestheader ("Content-type","application/x-www-form-urlencoded"); //You must take this parameter, even if the method you want to invoke does not have parameters, whether it is a get or post with a parameter, and no argument is sent to nullReq.send (""); //The callback function, when readyState equals 4 and the status is 200, indicates that the response is ready and can do the data operation.Req.onreadystatechange =function () {if(Req.readystate = =4) {                            if(Req.status = = $) {                                //Req.responsetext can return back to the backgroundAlert"Success"); } Else{alert ("Error"); }                        }                                            }                }            }//Get MethodfunctionGet() {                varreq =createxmlhttprequest (); if(req) {Req.open ("GET","ajaxtest.aspx",true); Req.onreadystatechange=function () {if(Req.readystate = =4) {                            if(Req.status = = $) {alert (req.responsetext); } Else{alert ("Error"); }}} req.send (NULL); }            }

Method Two:

 //take the Get method as an example            varXMLHTTP; function Loadxmldoc (URL, cfunc) {//Create an environment                if(Window. XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, SafariXMLHTTP =NewXMLHttpRequest (); }                Else{//code for IE6, IE5XMLHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); }                //To create a callback functionXmlhttp.onreadystatechange =Cfunc; //get mode to send dataXmlhttp.open ("GET"Urltrue);            Xmlhttp.send (); } function MyFunction () {//call the Ajax method described above, passing in the URL and the callback function implementation method (will be executed when the response is ready)Loadxmldoc ("/ajax/test1.txt", function () {if(Xmlhttp.readystate = =4&& Xmlhttp.status = = $) {document.getElementById ("mydiv"). InnerHTML =Xmlhttp.responsetext;            }                }); }

Native Ajax notation that can be copied directly to apply

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.