Summary of the Data parameter usage of Ajax in jquery

Source: Internet
Author: User
Summary of the Data parameter usage of Ajax in jquery
 
Jquery manual description:
 
Data
 
The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processdata option to disable automatic conversion. It must be in key/value format.
 
If it is an array, jquery automatically corresponds to the same name for different values. For example, {FOO: ["bar1", "bar2"]} is converted to '& Foo = bar1 & Foo = bar2 '.

 
Example:
 
$. Ajax ({type: "Post", URL: "Some. PHP ", data:" name = John & location = Boston ", success: function (MSG) {alert (" data saved: "+ MSG );}});
 
Here, the parameters following data can be written in two forms: one is to write common URL parameters, and the other is to write them in a JSON array,
 
The data section in the preceding example can also be written as follows: Data: {name: "John", Location: "Boston "}. What are the differences between the two statements?
Today, I found a slight difference in the usage of the two in development. First, we use the URL to pass the parameter. If the parameter contains the "&" symbol, the parameter may not be received or is incomplete, for example, "data: "Name = John & location = Boston ",",
 
If the name value is "John & Smith", this may cause a problem. We can escape it using the encodeuricomponent () method in Js,
 
However, if you use data: {name: "John", Location: "Boston"}, you do not need to escape it. If you escape it, you will receive the escaped string.

 
Jquery manual description:
 
Data
 
The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processdata option to disable automatic conversion. It must be in key/value format.
 
If it is an array, jquery automatically corresponds to the same name for different values. For example, {FOO: ["bar1", "bar2"]} is converted to '& Foo = bar1 & Foo = bar2 '.

 
Example:
$. Ajax ({type: "Post", URL: "Some. PHP ", data:" name = John & location = Boston ", success: function (MSG) {alert (" data saved: "+ MSG );}});
 
Here, the parameters following data can be written in two forms: one is to write common URL parameters, and the other is to write them in a JSON array,
 
The data section in the preceding example can also be written as follows: Data: {name: "John", Location: "Boston "}. What are the differences between the two statements?
 
Today, I found a slight difference in the usage of the two in development. First, we use the URL to pass the parameter. If the parameter contains the "&" symbol, the parameter may not be received or is incomplete, for example, "data: "Name = John & location = Boston ",",
 
If the name value is "John & Smith", this may cause a problem. We can escape it using the encodeuricomponent () method in Js,
 
However, if you use data: {name: "John", Location: "Boston"}, you do not need to escape it. If you escape it, you will receive the escaped string.
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.