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 (/</g, ' < ').
Replace (/>/g, ' > '). Replace ("<", "<").
Replace (/&/g, "&"). Replace (/&/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