-
1,Content-Type
很多时候无法解析就是Content-Type的问题。
如果本身就是xml文件,请跳过这一步
动态生成的XML一定要将其设置为text/xml,否则默认就是text/html也就是普通的文本了。
常见语言的Content-Type设置CODE:header("Content-Type:text/xml"); //php
response.ContentType="text/xml" //asp
response.setHeader("ContentType","text/xml"); //jsp
- 2,解析
- $.ajax({
- url:'ajax.asp',
- type: 'GET',
- dataType: 'xml',//这里可以不写,但千万别写text或者html!!!
- timeout: 1000,
- error: function(xhr){
- alert('Error loading XML document:ajax.asp\nHttp status: " + xhr.status + " " + xhr.statusText);
- },
- success: function(xml){
- $(xml).find("student").each(function(i){
- var id=$(this).children("id"); //取对象
- var id_value=$(this).children("id").text(); //取文本
- alert(id_value);//这里就是ID的值了。
- alert($(this).attr("email")); //这里能显示student下的email属性。
- $('<li></li>')
- .html(id_value)
- .appendTo('ol');
- });
- }
- });