Understand the DataType attribute option value in jquery Ajax _ajax related

Source: Internet
Author: User
Tags script tag

The AJAX datatype attribute in jquery is used to specify the type of data returned by the server, and if not, jquery automatically intelligently judges against the HTTP packet mime information, and if the datatype option is not filled in, the returned data is treated as a string.

One, Ajax syntax

Copy Code code as follows:

Jquery.ajax ([Settings])

Parameter description

Settings: A collection of key-value pairs used to configure AJAX requests. You can set the default value for any option by $.ajaxsetup ().

Second, the AJAX datatype option value

1. "XML": Returns an XML document that can be processed with jQuery.

2, "HTML":: Returns the plain text HTML information, the containing script label executes when inserting the DOM.

3, "script":: Return the plain text JavaScript code. Results are not automatically cached unless the "cache" parameter is set. Note: On remote requests (not in the same domain), all POST requests are converted to get requests. (because it will be loaded using the DOM's script tag)

4, "JSON": Returns the JSON data.

5, "JSONP": Jsonp format. When calling a function using JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? The correct function name to perform a callback function that is often used to cross domain requests under different two-level domain names under the primary domain name.

6, "text": Returns a plain text string.

The following is a datatype related issue in jquery Ajax

When datatype is set to HTML, it returns only plain text, nothing to say, and when set to XML, it returns an XML Document object.
Like return to the following XML

< XML version= "1.0" encoding= "Utf-8" >
<documentElement>
<table>
<provinceID> 110000</provinceid>
<province> Beijing </province>
<provinceid>120000</provinceid >
<province> Tianjin </province>
</table>
</documentElement>

This needs to be done with XmlDocument, and also to consider the browser
In fact, jquery itself can parse XML as a simple example.

$.ajax ({
type: "Get",
dataType: "xml",
URL: "Test.xml",
success:function (datas) {
$ ("province" , datas). Each (function (i) { 
alert ($ ($ (' Provinceid ', datas) [i]). Text () + "_" + $ (This). text ());
});

Test.xml content is the above XML, so Datas is the return of the XML Document object, LZ can try their own

In fact, I prefer to use datatype: "JSON", and using JSON data is easier for JavaScript to parse.

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.