Full AJAX notation (multi-browser support)

Source: Internet
Author: User
Tags response code

The code is as follows:

<script type= "Text/javascript" >varXMLHTTP; functionSubmit () {//1. Create a XMLHttpRequest object        if(window. XMLHttpRequest) {//Ie7,ie8,firefox, otherXMLHTTP =NewXMLHttpRequest (); if(xmlhttp.overridemimetype) {//bug fixes for certain specific versions of Mozillar browserXmlhttp.overridemimetype ("Text/xml"); }                   } Else if(window. ActiveXObject) {//ie6,ie5.5,ie5XMLHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); }        //simple notation, should also be able to    //xmlhttp = new XMLHttpRequest () | | new ActiveXObject ("Microsoft.XMLHTTP");        if(xmlhttp = = Undefined | | xmlhttp = =NULL) {alert (' The current browser does not support the creation of XMLHttpRequest objects, please replace the browser! ‘); return; }        //save each state to an arrayArray.push (xmlhttp.readystate); //2. Registration callback method CallbackXmlhttp.onreadystatechange =callback; //Get Client Content        varUserName = document.getElementById ("UserName"). Value; /*//get Request mode//3. Set the corresponding parameters of the server-side interaction Xmlhttp.open ("Get", "Ajaxrequst.ashx?name=" +username, true);        4. Set to send data to the server, start and service side interactive xmlhttp.send (); */        //Post Request Mode        //3. Setting the appropriate parameters for server-side interactionXmlhttp.open ("Post", "Ajaxrequst.ashx",true); //The Post method requires additional codeXmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); //4. Set up to send data to the server, start and service side interactionXmlhttp.send ("name=" +userName); }    functioncallback () {//5. Determine whether the server side of the interaction is complete, but also to determine whether the service side of the correct return of the data        if(Xmlhttp.readystate = = 4) {            //indicates that the interaction with the server has been completed            if(Xmlhttp.status = = 200) {                //indicates that the server's response code is 200, and the correct return data                //How to accept pure file data                varMessage =Xmlhttp.responsetext; //Add the returned content to the DIV layer                vardiv = document.getElementById (' message ')); Div.innerhtml=message; //How to accept the DOM object corresponding to the XML data                //The premise is that the server side needs to be set Content-type to Text/xml                //var domxml = xmlhttp.responsexml;                            }        }    }    </script>

Full AJAX notation (multi-browser support)

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.