Jquery Ajax parse XML data (synchronous and asynchronous invocation) Simple instance _jquery

Source: Internet
Author: User

Copy Code code as follows:

$.ajax ({
Async:true,//default True (asynchronous request)
Cache:true,//default True, set to False will not load request information from the browser cache.
Type: "POST",//default: Get request mode: [Post/get]
DataType: "xml",//default ["XML"/"HTML"] returns data type: ["XML"/"HTML"/"script"/"JSON"/"JSONP"]
URL: "Test.ashx",//default current address, send the requested address
Data: {key: ' value '},//sent to the server
Error:function (XML) {alert (' Error loading XML document ' + XML);}, called when//request failed
timeout:1000,//Set Request timeout
Success:function (XML) {//Request callback function parameter after success: Server returns data, data format.
$ ("#users"). empty ();
Using 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 "); Fetching objects
var id_value=$ (this). Children ("id"). text (); Fetching text
alert (id_value);//This is the value of the ID.
Alert ($ (this). attr ("email")); Here you can display the email attribute under student.

The final output, this is Cssrain, seemingly more jq than Macnie
$ (' <li></li> '). HTML (id_value). Appendto (' ol ');
});
*/
}
})


Returns XML data using the Ashx file:
Copy Code code 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;
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.