Simple Example of Jquery Ajax parsing XML data
This article mainly introduces simple examples of Jquery Ajax parsing XML data (synchronous and asynchronous calls). If you need some help, please refer to them and hope to help you.
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 ("<li>" + loginname + "-" + name + "</li> ");
});
/*
$ (Xml). find ('user'). each (function (I ){
Var loginname = $ (xml). find ("user loginname"). eq (I). text ();
Var user name "). eq (I). text ();
$ ("# Users "). append ("<p>" + loginname + "</p>" + "<p>" + name + "</p> <Br/> ");
})
$ (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.
$ ('<Li> </li> 'hangzhou.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 ("<? Xml version = "1.0" encoding = "gbk"?> N "+ ds. GetXml ());
/*
StringBuilder sb = new StringBuilder ();
Sb. Append ("<? Xml version = "1.0" encoding = "gbk"?> ");
Sb. Append ("<AccountList> ");
Sb. Append ("<Account> <loginname> Loro5 </loginname> <name> wulu </name> </user> ");
Sb. Append ("</Account> ");
Context. Response. Write (sb. ToString ());
*/
Context. Response. End ();
}
Public bool IsReusable {
Get {
Return false;
}
}
}