Common AJAX Requests

Source: Internet
Author: User

jquery version of Ajax request: (including processing of XML strings in WebService)

1 $.ajax ({2Type: "POST",3Asynctrue,4URL: "",5Data: "",6Successfunction(data) {7data = Data.replace ("<string xmlns=\" http://tempuri.org/\ ">" "").
Replace ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>").
Replace ("</string>", ""). Replace ("undefined", "").
Replace (";", ""). Replace (/&lt;/g, ' < ').
Replace (/&gt;/g, ' > '). Replace ("&lt", "<").
Replace (/&amp;/g, "&"). Replace (/&amp/g, "&").
Replace (/\n/g, "").
Replace (/\r/g, "");8 9 },TenErrorfunction () { One A }, -DataType: "HTML" -});

JS version of the AJAX request:

Common.js

//because the browser version affects Ajax differently, encountering different versions requires a different Ajax//Create an Ajax objectfunctioncreatexmlhttp () {varXhobj =false; Try{xhobj=NewActiveXObject ("Msxml2.xmlhttp");//ie msxml3.0+}Catch(e) {Try{xhobj=NewActiveXObject ("Microsoft.XMLHTTP");//ie msxml2.6}Catch(E2) {Xhobj=false; }    }    if(!xhobj &&typeofXMLHttpRequest! = ' undefined ') {//Firefox, Opera 8.0+, SafariXhobj =NewXMLHttpRequest (); }    returnxhobj;}

Body:

Get commit:

<!--introduced to Common.js--<script src= "Scripts/common.js" type= "Text/javascript" ></script> <script t Ype= "Text/javascript" >varAJ =false; Window.onload=function () {            //New an AjaxAJ =createxmlhttp (); }        //Ajax function Get commit        functionDoajax () {//Open Connection            //you need to use multiple parameters, the first to set the method property, the second to set the destination URL, and the third to specify whether to synchronize (false) or asynchronous (true) to send the request            varurl = ""; Aj.open ("GET", URL,true); //set the callback function [i.e., the value returned by the server needs to be accepted]            //Read State changeAj.onreadystatechange =function() {alert (aj.readystate); if(Aj.readystate >= 4) {                    if(Aj.status = = 200) {//status code is 200 normal response

} Else {

} } }; //send [get Send as empty]Aj.send (NULL); } </script>

Post submission:

<script src= "Scripts/common.js" type= "Text/javascript" ></script> <script type= "Text/jscript" >varAJ =false; Window.onload=function() {AJ=createxmlhttp (); }        //Ajax function Post submission        functionDoajax () {varurl = "Js_login.aspx"; //if the submitted value is in Chinese, you need to encode            //encodeURI () or encodeuricomponent ()            varuser = encodeURI (Gel ("TXT")). Value); varPWD = Gel ("pwd"). Value; vardata = "user=" + user + "&pwd=" +pwd; //Open ConnectionAj.open ("POST", URL,true); //need to set the request headerAj.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); //callback functionAj.onreadystatechange =function () {                if(Aj.readystate >= 4) {                    if(Aj.status = = 200) {//status code is 200 normal response                       vartxt = aj.responsetext;//Accept Data                      }
Else {
} } } //send data [post send cannot be empty]aj.send (data); } </script>

Common AJAX Requests

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.