Understanding the option value of datatype attribute in jQuery Ajax


The datatype attribute of Ajax in jQuery is used to specify the data type returned by the server. If not specified, jQuery will automatically make intelligent judgment based on the mime information of HTTP packets. If the datatype option is not filled in, the returned data will be treated as a string.

1、 Ajax syntax

Copy codeThe code is as follows:

Parameter description

Settings: a collection of key value pairs used to configure Ajax requests. You can set the default value of any option through $. Ajaxsetup().

2、 The value of the datatype option of Ajax

1. “XML”: return the XML document, which can be processed by jQuery.

2. “HTML”: returns plain text HTML information; the included script tag is executed when the DOM is inserted.

3. “Script”: returns plain text JavaScript code. Results are not automatically cached unless the cache parameter is set. Note: on remote requests (not under the same domain), all post requests will be converted to get requests. (because the DOM’s script tag will be used to load)

4. “JSON”: returns JSON data.

5. “Jsonp”: jsonp format. When calling functions in jsonp form, such as “myurl? Callback =? JQuery will automatically replace? With the correct function name to execute callback functions. It is often used for cross domain requests under different secondary domain names under the same primary domain name.

6. “Text”: returns a plain text string.

Here are some questions about datatype in jQuery Ajax

When datatype is set to HTML, only plain text is returned. There is nothing to say. When datatype is set to XML, it returns an XML document object,
For example, return the following XML

< xml version="1.0" encoding="utf-8" >
< province > Beijing
< province > Tianjin

This requires the use of XML document and other operations, but also consider the browser
In fact, jQuery itself can parse XML, for example

type: "Get",
dataType: "xml",
url: "test.xml",
success: function(datas){
alert($($("provinceID",datas)[i]).text() + "_" + $(this).text());

test.xml The content is the above XML, so the data is the returned XML document object. LZ can try it on its own

In fact, relatively speaking, I prefer to use datatype: “JSON”. Using JSON data is more convenient for JavaScript to parse

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]