Precautions for passing values in jQueryget and post Methods _ jquery

Source: Internet
Author: User
As jQuery knows, 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? V11 & amp; v22), and the second parameter can be omitted, that is, the second parameter can directly write the callback function. What is the difference between writing data after the address and writing data in the data parameter? 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

The Code is as follows:


Echo "post :";
Print_r ($ _ POST );
Echo "get :";
Print_r ($ _ GET );
?>


Jquery_test.html
Lab 1:

The Code is as follows:


$ (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, using 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 with 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 more solid.

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.