1, Content-Type
In many cases, the Content-Type issue cannot be resolved.
Skip this step if it is an XML file.
The dynamically generated XML must be set to text/XML. Otherwise, text/html is the common text by default.
Set Content-Type in common languages
- Header ("Content-Type: text/XML"); // PHP
- Response. contenttype = "text/XML" // ASP
- Response. setheader ("contenttype", "text/XML"); // JSP
|
2. XML structure.
XML must be closed. It is very important!
Example:
Incorrect XML
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Name> zhangsan </Name>
- <ID> 1 </ID>
- <Name> Lisi </Name>
- <ID> 2 </ID>
|
Correct
-
- <? XML version = "1.0" encoding = "UTF-8"?>
-
- <Stulist>
-
- <Student email = "1@1.com">
-
- <Name> zhangsan </Name>
- <ID> 1 </ID>
-
- </Student>
-
- <Student email = "2@2.com">
-
- <Name> Lisi </Name>
-
- <ID> 2 </ID>
-
- </Student>
-
- </Stulist>
|
3. Parsing
Here, macnie's
Traverse student (here we still use the above XML and the sub-node is student)
-
- $. Ajax ({
-
- URL: 'ajax. asp ',
- Type: 'get ',
-
- Datatype: 'xml', // do not write text or HTML here !!!
-
- 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"); // retrieves an object
-
- VaR id_value = $ (this). Children ("ID"). Text (); // get text
- Alert (id_value); // here is the ID value.
-
- Alert ($ (this). ATTR ("email"); // The email attributes under student are displayed here.
-
-
- // The final output. This is the CSS rain method, which seems to be a little more JQ than macnie.
-
- $ ('<Li> </LI> ')
-
- . Html (id_value)
-
- . Appendto ('ol ');
-
- });
-
- }
-
- });
|
4. Disable Cache
If you directly use the Ajax method, you can use cache: false to disable caching.
Note: cache: falseInstead of cache: "false ". The Boolean value false is not the string's "false ".
If you use the get or POST method, you can add a timestamp after the URL. For example, "XML. php? TS "+ (+ new date)
Note: Do not use random numbers.You cannot predict whether the random number will be random again ......
But under normal circumstances, the timestamp is certainly not repeated.
The last one is added: ensure that the server side is UTF-8 encoded, otherwise garbled!
Well, it's not complicated to complete, right?