Get and Post methods in JQuery Considerations for passing values

Source: Internet
Author: User

jquery knows that the get and post methods of jquery have three parameters: address, data, and callback functions, but we know that the address can also follow the data (shape: 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, so what is the difference between the data written at the address and written in the data parameter?

Just done a few experiments, and see the following code is clear:
The following needs to be answered to see

jquery_data.php

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

Jquery_test.html

Lab 1:

$ (function () {
Post method with data in both places
$.post (' Jquery_data.php?v1=1 ', {v2:2}, function (data) {
$ ('

'). Append (data). AppendTo (' body ');
});
});

return Result:
Post:array
(
[v2] = 2
)
Get:array
(
[V1] = 1
)

Lab 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 ');
});
});

The result is returned, and 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)
{
$ (' <pre/> '). Append (data). AppendTo (' body ');
});
});

The result is returned, and the data is in get:
Post:array
(
)
Get:array
(
[v2] = 2
)

Lab 4:

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

The results are returned, both data are merged and are in Get:
Post:array
(
)
Get:array
(
[V1] = 1
[v2] = 2
)

Lab 5:

$ (function ()
{
Get method with data in both places with the same variable name
$.get (' Jquery_data.php?v2=1 ', {v2:2}, function (data)
{
$ (' <pre/> '). Append (data). AppendTo (' body ');
});
});

The result is returned, the data is in get, and the data in the DataSource overwrites the data following the address:
Post:array
(
)
Get:array
(
[v2] = 2
)
It is easy to see from these simple small examples that the data behind the address is always passed in get form, regardless of whether the get method or the Post method is used, and the data in this parameter is passed according to the method.

Therefore, in order to avoid confusion, we recommend that you try not to write the data at the end of the address, but uniformly put it in the data parameter.

Of course, if you want to use the get pass when you use the Post method, you can write the data that you want to pass in the get to the address, and write the data that you want to pass as a post in the database parameter.

In short, the method is dead, people are alive, how to use also to see the actual situation. The son once said: Practice is the only standard to test truth. Nothing to do the experiment, grasp the knowledge more solid.
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.