Home  >  Article  >  Web Front-end  >  Notes on passing values ​​in jQuery get and post methods_jquery

Notes on passing values ​​in jQuery get and post methods_jquery

WBOY
WBOYOriginal
2016-05-16 18:42:341023browse

I just did a few experiments, and it will be clear when you look at the following code:
The following content requires a reply to see
jquery_data.php

Copy code The code is as follows:

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

jquery_test.html
Experiment 1:
Copy Code The code is as follows:

$(function()
{
// post method, there is data in both places
$.post ('jquery_data.php?v1=1', {v2: 2}, function(data)
{
$('
').append(data).appendTo('body' ); 
});
});
/*

Return results:
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)
{
$('
').append(data).appendTo('body'); 
});
});
/*
Return the result, the data is in get Medium:
post: Array
(
)
get: Array
(
[v1] => 1
)
*/
Experiment 3 :
$(function()
{
// get method, use data parameter to pass value
$.get('jquery_data.php', {v2: 2}, function(data)
{
$('
').append(data).appendTo('body'); 
});
});
/*
Return the result, the data is in get:
post: Array
(
)
get: Array
(
[v2] => 2
)
* /
Experiment 4:
$(function()
{
// get method, there is data in both places
$.get('jquery_data.php?v1=1', { v2: 2}, function(data)
{
$('
').append(data).appendTo('body'); 
});
} );
/*
Return the result, the two data are merged, both in get:
post: Array
(
)
get: Array
(
[v1] => 1
[v2] => 2
)
*/
Experiment 5:
$(function()
{
/ / get method, there is data in both places, and the variable names are the same
$.get('jquery_data.php?v2=1', {v2: 2}, function(data)
{
$( '
').append(data).appendTo('body'); 
});
});
/*
Return the result, the data is in get, And the data in the data parameter covers the data after the address:
post: Array
(
)
get: Array
(
[v2] => 2
)
*/
It is not difficult to see from these simple examples that the data behind the address is always passed in the form of get, regardless of whether the get method or the post method is used; and the data in the data parameter is The delivery method is determined based on the method.
Therefore, in order to avoid confusion, it is recommended that you try not to write the data after the address, but place it uniformly in the data parameter.
Of course, if you want to use get to pass values ​​when using the post method, then you can write the data to be passed in the get method after the address, and the data to be passed in the post method in the data parameter.
In short, methods are dead and people are alive. How to use them depends on the actual situation. Zi once said: Practice is the only criterion for testing truth. Do experiments when you have nothing to do, and master the knowledge more firmly.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn