代码改变世界

jquery ajax xml

2010-09-08 08:38  爱研究源码的javaer  阅读(298)  评论(0编辑  收藏  举报
:
function Resultxml()
{
   $.ajax
   ({
    url: 'ajaxRespose.aspx',
    data: {id: 1, name: $('#gender').val()},
   dataType: 'xml',
    success: function(xml)
     {
        alert(xml);
       
        $(xml).find('select').each(function()
        {
            var option1 = "<OPTION value='";  
            var option2 = "'>";  
            var option3 = "</OPTION>";
            var text = $(this).children("text").text();
            var value = $(this).children("value").text();
            $("#city").append(option1 + value + option2 + text + option3);
         
        });

     }
   });
}
服务端返回的xml文件
 String targetId = Request.QueryString["id"]; // 获得请求中参数为id的值
            String xml_version = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            String xml_start = "<selects>";
            String xml_end = "</selects>";
            String xml = "";

            switch (targetId)
            {
                case "0":
                    xml = "<select><value>0</value><text>请选择</text></select>";

                    break;
                case "1":
                    xml = "<select><value>1</value><text>昌平</text></select>";
                    xml += "<select><value>2</value><text>丰台</text></select>";
                    xml += "<select><value>3</value><text>海淀</text></select>";
                    xml += "<select><value>4</value><text>朝阳</text></select>";
                    break;
                case "2":
                    xml = "<select><value>1</value><text>塘沽区</text></select>";
                    xml += "<select><value>2</value><text>汉沽区</text></select>";
                    xml += "<select><value>3</value><text>大港区</text></select>";
                    xml += "<select><value>4</value><text>东丽区</text></select>";
                    break;
                case "3":
                    xml = "<select><value>1</value><text>济南</text></select>";
                    xml += "<select><value>2</value><text>青岛</text></select>";
                    xml += "<select><value>3</value><text>淄博</text></select>";
                    xml += "<select><value>4</value><text>枣庄</text></select>";
                    break;
            }

            String last_xml = xml_version+xml_start + xml + xml_end;
            Response.ContentType = "text/xml";//返回数据类型为xml(必须的)
            Response.Write(last_xml);
            Response.End();

在Microsoft Visual Studio 2005 ,IE6.0下测试通过

转自:http://www.cnblogs.com/chenghm2003/archive/2008/08/11/1265263.html