This article mainly introduces a simple example of JqueryAjax parsing XML data (synchronous and asynchronous calls). If you need some help, please refer to it.
The Code is as follows:
$. Ajax ({
Async: true, // default value: true (asynchronous request)
Cache: true, // The default value is true. setting this parameter to false will not load request information from the browser cache.
Type: "POST", // default: GET Request Method: [POST/GET]
DataType: "xml", // default ["xml"/"html"] return data type: ["xml"/"html"/"script"/"json"/"jsonp"]
Url: "Test. ashx", // The default current address, the address that sends the request
Data: {key: "value"}, // data sent to the server
Error: function (xml) {alert ('error loading XML document' + xml) ;}, // called when the request fails
Timeout: 1000, // set the request timeout
Success: function (xml) {// callback function parameter after the request is successful: data returned by the server, data format.
$ ("# Users"). empty ();
// Use Jquery to process xml data
$ (Xml). find ('table'). each (function (){
Var loginname = $ (this). find ("Loginname"). text ();
Var Name "). text ();
$ ("# Users"). append ("
"+ Loginname +"-"+ name +"");
});
/*
$ (Xml). find ('user'). each (function (I ){
Var loginname = $ (xml). find ("user loginname"). eq (I). text ();
Var user name "). eq (I). text ();
$ ("# Users"). append ("
"+ Loginname +"
"+"
"+ Name +"
");
})
$ (Xml). find ("student"). each (function (I ){
Var id "); // retrieves an object
Var id_value = $ (this). children ("id"). text (); // get text
Alert (id_value); // here is the ID value.
Alert ($ (this). attr ("email"); // The email attributes under student are displayed here.
// The final output is shown. This is the CSS rain method, which seems to be a little more JQ than macnie.
$ ('
'0000.html (id_value). appendTo ('ol ');
});
*/
}
})
Use the ashx file to return XML data:
The Code is as follows:
<% @ WebHandler Language = "C #" %>
Using System;
Using System. Web;
Using System. Text;
Using System. Data;
Public class Test: IHttpHandler {
Public void ProcessRequest (HttpContext context ){
Context. Response. StatusCode = 200;
Context. Response. Cache. SetCacheability (HttpCacheability. NoCache );
DataSet ds = new DataSet ("AccountList ");
Ds = GetList ("Account", "AccountId", "Loginname, Name", 50, 1, false, false, "1 = 1 ");
Context. Response. ContentType = "text/xml ";
Context. Response. Charset = "GB2312 ";
Context. Response. Clear ();
Context. Response. Write (" \ N "+ ds. GetXml ());
/*
StringBuilder sb = new StringBuilder ();
Sb. Append (" ");
Sb. Append ("");
Sb. Append (" Loro5 Wulu ");
Sb. Append ("");
Context. Response. Write (sb. ToString ());
*/
Context. Response. End ();
}
Public bool IsReusable {
Get {
Return false;
}
}
}