Home > Web Front-end > JS Tutorial > body text

Use JQuery to implement AJAX script to load XML and parse it_jquery

WBOY
Release: 2016-05-16 18:49:29
Original
979 people have browsed it
1,Content-Type
Many times the failure to parse is a problem with Content-Type.
If it is an xml file, please skip this step
The dynamically generated XML must be set to text/xml, otherwise it will default to text/html, which is ordinary text.
Content-Type settings for common languages
Copy code The code is as follows:

header( "Content-Type:text/xml"); //php
response.ContentType="text/xml" //asp
response.setHeader("ContentType","text/xml"); //jsp

2, xml structure.

Example:
Wrong XML
Copy code The code is as follows:

CODE:

zhangsan1
lisi
2
Correct
CODE:



zhangsan
1


lisi< ;/name>
2



3, parse
Here refers to macnie's
traversing students (the above XML is still used here, the child node is student)
Copy code The code is as follows:

$.ajax({
url:'ajax.asp',
type: 'GET',
dataType: 'xml',//here You don’t have to write it, but don’t write text or html!!!
timeout: 1000,
error: function(xml){
alert('Error loading XML document' xml);
} ,
success: function(xml){
$(xml).find("student").each(function(i){
var id=$(this).children("id") ; //Get the object
var id_value=$(this).children("id").text(); //Get the text
alert(id_value);//Here is the value of the ID.
alert($(this).attr("email")); //The email attribute under student can be displayed here.
//The last output, this is the writing method of cssrain, which seems to be more elegant than macnie
$('
  • ')
    .html(id_value)
    .appendTo('ol');
    });
    }
    });

    One last thing to add: Make sure the server is encoded in utf-8
    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template