Home >Web Front-end >JS Tutorial >Jquery Ajax parsing XML data (synchronous and asynchronous calls) simple example_jquery

Jquery Ajax parsing XML data (synchronous and asynchronous calls) simple example_jquery

WBOY
WBOYOriginal
2016-05-16 17:00:381109browse

Copy code The code is as follows:

$.ajax({
                async: true, // 默认true(异步请求)
                cache: true, // 默认true,设置为 false 将不会从浏览器缓存中加载请求信息。
                type: "POST", // 默认:GET 请求方式:[POST/GET]
                dataType: "xml", //默认["xml"/"html"] 返回数据类型:["xml" / "html" / "script" / "json" / "jsonp"]
                url: "Test.ashx", // 默认当前地址,发送请求的地址
                data: { key: "value" }, // 发送到服务器的数据
                error: function(xml) { alert('Error loading XML document' + xml); }, // 请求失败时调用
                timeout: 1000, // 设置请求超时时间
                success: function(xml) { // 请求成功后回调函数 参数:服务器返回数据,数据格式.
                    $("#users").empty();
                    // 用Jquery处理xml数据
                    $(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"); //取对象
                            var id_value=$(this).children("id").text(); //取文本
                            alert(id_value);//这里就是ID的值了。
                            alert($(this).attr("email")); //这里能显示student下的email属性。

    // The final output. This is the writing of CSSRAIN. It seems that it is more jq than MacNie. ;
                                                                                                    XML data:




    Copy code



    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("");
    sb.Append ("");
    sb.Append("Loro5wulu");
    sb.Append("
    ");

    context.Response.Write(sb.ToString());

    */


    context.Response.End();

    }

    public bool IsReusable {

    get {

    return false;
    }

    }


    }


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn