Understanding the datatype attribute option value in jquery ajax, jquerydatatype
The dataType attribute of ajax in jquery is used to specify the data type returned by the server. If this parameter is not specified, jQuery automatically determines Based on the MIME information of the HTTP package. If the datatype option is not specified, the returned data is processed as a string.
I. ajax syntax
Copy codeThe Code is as follows:
JQuery. ajax ([settings])
Parameter description
Settings: set of key-value pairs used to configure Ajax requests. You can use $. ajaxSetup () to set the default values of any options.
Ii. Value of the datatype option of ajax
1. "xml": returns an XML document, which can be processed by jQuery.
2. "html": returns plain text HTML information. The script tag is executed when the dom is inserted.
3. "script": return plain text JavaScript code. Results are not automatically cached unless the "cache" parameter is set. Note: During remote requests (not in the same domain), all POST requests are converted to GET requests. (Because the script tag of DOM will be used for loading)
4. "json": Return JSON data.
5. "jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function, it is often used for cross-origin requests under different second-level domain names under the same primary domain name.
6. "text": returns a plain text string.
The following are some questions about datatype in jquery ajax:
When datatype is set to html, only plain text is returned, which is nothing to mention. When it is set to xml, an xml document object is returned,
For example, the following xml is returned.
<Xml version = "1.0" encoding = "UTF-8"> <documentElement> <table> <provinceID> 110000 </provinceID> <province> Beijing </province> <provinceID> 120000 </provinceID> <province> Tianjin </province> </table> </documentElement>
This requires the use of XMLDocument and other operations, but also consider the browser
In fact, jquery can parse xml itself. For example
$.ajax({type: "Get",dataType: "xml",url: "test.xml",success: function(datas){$("province",datas).each(function(i){ alert($($("provinceID",datas)[i]).text() + "_" + $(this).text());});}});
The content in test. xml is the above xml, so that datas is the returned xml Document Object. lz can try it on its own.
In fact, I prefer datatype: "json". It is easier for javascript to parse json data.