The previous paragraph is a piece of work because of the need to write a JavaScript package to give the tool class, technology is limited, Miss Spray, thank you for your support.
1. The following is the JavaScript Ajax tool class.
functioncreatexmlhttprequest () {varreq; if(window. XMLHttpRequest) {//compatible with browsers that are non-IE compatible and IE7req =NewXMLHttpRequest (); }Else if(window. ActiveXObject) {//available in Internet Explorer 5.5 and later versions Try{req=NewActiveXObject ("Microsoft.XMLHTTP"); } Catch(e) {//available in Internet Explorer 6req =NewActiveXObject ("Msxml2.xmlhttp"); } } returnreq;}/*parameter Description: Method: Method of submission (get,post) URL: Requested path param: parameter to pass async: Whether a step type: return value type (Xml,json, default string) fn200: is a function Things to do after a successful request fn404: is a function processing request failed to report 404 error fn500: is a function processing request failed to report 500 error*/functionSendajaxreq (method,url,param,async,type,fn200,fn404,fn500,loading) {varreq =createxmlhttprequest (); //2. Define the processing responseReq.onreadystatechange =function (){ if(Req.readystate = = 4){ if(Req.status = = 200){ if(fn200) {varresult; if(NULL! = Type && ' xml ' = =type.tolowercase ()) {Result=Req.responsexml; }Else if(NULL! = Type && ' json ' = =type.tolowercase ()) {Jsonstr=Req.responsetext; if(document.all) {result= eval (' (' + jsonstr + ') '); }Else{result=Json.parse (JSONSTR); } }Else{result=Req.responsetext; } fn200 (Result); } }Else if(Req.status = = 404){ if(fn404) {fn404 (); } }Else if(Req.status = = 500){ if(fn500) {fn500 (); } } }Else{ if(Loading) {loading (); } } }; if(' get ' = =method.tolowercase ()) {Req.open (method, URL+ (param = =NULL? ‘‘ : ‘?‘ +param), async); Req.send (NULL); }Else if(' post ' = =method.tolowercase ()) { //1. Define whether the requested address is asynchronous in the way the request is sent;Req.open (method, URL, async); Req.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Req.send (param); } }
2. Call Mode:
functionTest () {varurl = "Test?date=" +NewDate (); Sendajaxreq ("Get", url,NULL,true, ' JSON ',function(Result) {//here is the callback method after the request succeeds, can do the processing after the request succeeds, result is the parameter returned by the background},function() {//Here you can jump to a 404 page},function() {//here is the handling of the 500 error},function() {//dealing with other issues});}
The Ajax tool class based on JavaScript encapsulation