JQuery get and Post methods for value-passing considerations _jquery

Source: Internet
Author: User
Just did a few experiments, look at the following code is clear:
The following items need to be answered to see
jquery_data.php
Copy Code code as follows:

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

Jquery_test.html
Experiment 1:
Copy Code code as follows:

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

return Result:
Post:array
(
[v2] => 2
)
Get:array
(
[V1] => 1
)
*/
Experiment 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)
{
$ (' <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, passing values with the data parameter
$.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 with data in both places
$.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 the same variable name
$.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.