Jquery Ajax 분석 XML 데이터 (동기 화 및 비동기 호출) 간단 한 인 스 턴 스

 
  
$.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 。

                            // , cssrain , macnie JQ
                            $('

  • ').html(id_value).appendTo('ol');
                        });
                        */
                    }
                })

  • 用ashx文件返回XML数据:
     
      

    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("
    " + 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;
            }
        }

    }

    좋은 웹페이지 즐겨찾기