Test the get and post methods in jQuery and precautions _ jquery

Source: Internet
Author: User
JQuery's get and post methods have three parameters: Address, data, and callback functions. I have just done a few experiments and read the following code to understand that jQuery is used, jQuery's get and post methods have three parameters: Address, data, and callback functions, but we know that the address can also follow the data (such as: get_data.php? V1 = 1 & v2 = 2), and the second parameter can be omitted. That is, the second parameter can be directly written to the callback function, what is the difference between writing data after an address and writing data parameters?
I have just done a few experiments and read the following code:
The following content must be replied before you can see

Jquery_data.php

echo "post: ";print_r($_POST);echo "get: ";print_r($_GET);?>

Jquery_test.html

Lab 1:

$ (Function () {// post method, both of which have data $. post ('jquery _ data. php? V1 = 1', {v2: 2}, function (data) {$ (''). append (data). appendTo ('body ');});});

Returned results:

post: Array([v2] => 2)get: Array([v1] => 1)

Lab 2:

$ (Function () {// post method, the data is behind the address, and the second parameter is the callback function $. post ('jquery _ data. php? V1 = 1', function (data) {$ ('
'). Append (data). appendTo ('body ');});});

The returned result. The data is in get:

post: Array()get: Array([v1] => 1)

Lab 3:

$ (Function () {// get method, use the data parameter to pass the value $. get ('jquery _ data. php ', {v2: 2}, function (data) {$ ('
'). Append (data). appendTo ('body ');});});

The returned result. The data is in get:

post: Array()get: Array([v2] => 2)

Tutorial 4:

$ (Function () {// get method, both of which have data $. get ('jquery _ data. php? V1 = 1', {v2: 2}, function (data) {$ ('
'). Append (data). appendTo ('body ');});});

The returned results are merged in the get:

post: Array()get: Array([v1] => 1[v2] => 2)

Lab 5:

$ (Function () {// get method, both of which have data and the same variable name $. get ('jquery _ data. php? V2 = 1', {v2: 2}, function (data) {$ ('
'). Append (data). appendTo ('body ');});});

The returned result is that the data is in get, and the data in the data parameter overwrites the data after the address:

post: Array()get: Array([v2] => 2)

From these few simple examples, it is not difficult to see that the data after the address is always transmitted in the form of get, whether the get method or post method is used; the data in the data parameter decides the transmission mode based on the method.

Therefore, to avoid confusion, we recommend that you do not write the data behind the address, but put it in the data parameter.

Of course, if you want to use the post method and get to pass the value, you can write the data to be passed in the get method after the address, write the data to be passed in post mode in the data parameter.

In short, the method is dead, people are active, and how to use it depends on the actual situation. I once said that practice is the only criterion for testing truth. There is nothing to do with the experiment, and the knowledge is better.

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.