Code
$. Ajax ({
Async: True , // Default Value: true (asynchronous request)
Cache: True , // The default value is true. If it is set to false, request information is not loaded from the browser cache.
Type: " Post " , // Default: GET Request Method: [post/get]
Datatype: " XML " , // ["XML"/"html"] return data type by default: ["XML"/"html"/"script"/"JSON"/"jsonp"]
URL: " Test. ashx " , // The default current address, the address for sending the request
Data: {key: " Value " }, // Data sent to the server
Error: Function (XML) {alert ( ' Error loading XML document ' + XML );}, // Call when the request fails
Timeout: 1000 , // Set Request timeout
Success: Function (XML ){ // Callback function parameters after successful request: the data returned by the server, in the 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:
Code
<% @ Webhandler Language = " C # " %>
using system;
using system. web;
using system. text;
using system. data;
Public ClassTest: ihttphandler {
Public VoidProcessrequest (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 (" ");
Sb. append (" ");
Sb. append (" loro5 wulu ");
Sb. append (" ");
context. response. write (sb. tostring ();
*/
Context. response. End ();
}
Public BoolIsreusable {
Get{
Return False;
}
}
}