Get and Post methods in jquery Pass-value test and precautions _jquery

Source: Internet
Author: User

As jquery knows, the Get and post methods for jquery have three parameters: address, data, and callback functions, but we know that addresses can also follow data (in the form of get_data.php?v1=1&v2=2), and the second argument can be omitted, That is, the second parameter can write the callback function directly, then what is the difference between the data written in the address and written in the parameters?
Just did a few experiments, look at the following code is clear:
The following items need to be answered to see

jquery_data.php

echo "Post:";
Print_r ($_post);
echo "Get:";
Print_r ($_get);
? >

Jquery_test.html

Experiment 1:

$ (function () {
//post method, both have data
$.post (' Jquery_data.php?v1=1 ', {v2:2}, function (data) {
$ ('
). Append (data). Appendto (' body ');});

return Result:

Post:array
(
[v2] => 2
)
Get:array
(
[v1] => 1
)

Experiment 2:

$ (function ()
{
//post method, the data is after the address, the second parameter is the callback function
$.post (' Jquery_data.php?v1=1 ', function (data)
{
$ (' <pre/> '). Append (data). Appendto (' body ');
});

Returns the result, and the data is in get:

Post:array
(
)
get:array
(
[v1] => 1
)

Experiment 3:

$ (function ()
{
//Get method, with the data parameter passed the value
$.get (' jquery_data.php ', {v2:2}, function (data)
{
$ (' <pre/> '). Append (data). Appendto (' body ');
};
});

Returns the result, and the data is in get:

Post:array
(
)
get:array
(
[v2] => 2
)

Experiment 4:

$ (function ()
{
//Get method, both have data
$.get (' Jquery_data.php?v1=1 ', {v2:2}, function (data)
{
$ ( ' <pre/> '). Append (data). Appendto (' body ');
};
});

Returns the result, two data are merged, all in Get:

Post:array
(
)
get:array
(
[v1] => 1
[v2] => 2
)

Experiment 5:

$ (function ()
{
//Get method, with data in both places, with variable names identical
$.get (' jquery_data.php?v2=1 ', {v2:2}, function (data)
{
$ (' <pre/> '). Append (data). Appendto (' body ');
});

Returns the result, where the data is in get, and the data in the parameter overwrites the data following the address:

Post:array
(
)
get:array
(
[v2] => 2
)

It is easy to see through these few simple examples that the data behind the address is always passed in get form, regardless of whether you are using a GET method or a post method, and the data in the parameters determines how it is passed.

Therefore, in order to avoid confusion, we suggest that we should try not to write the data behind the address, but to put it in the data parameter.

Of course, if you want to use the Post method and get a value at the same time, you can write the data that you want to pass in the get way behind the address and write the data that you want to post to be in the parameters.

In short, the method is dead, people are alive, how to use also depends on the actual situation. The son once said: Practice is the only standard to test truth. Nothing to do the experiment, grasp the knowledge more firmly.

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.