Understanding the datatype attribute option value in jquery ajax, jquerydatatype

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.