Home  >  Article  >  Web Front-end  >  Value-passing testing and precautions for get and post methods in jQuery_jquery

Value-passing testing and precautions for get and post methods in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:40:071069browse

Everyone who uses jQuery knows that jQuery’s get and post methods have three parameters: address, data and callback function, but we know that the address can also follow the data (in the form: get_data.php?v1=1&v2=2), And the second parameter can be omitted, that is, the second parameter can be written directly into the callback function. So what is the difference between writing data after the address and writing it in the data parameter?
I just did a few experiments, and it will be clear if you look at the following code:
The following content requires a reply to be seen

jquery_data.php

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

jquery_test.html

Experiment 1:

$(function() {
// post 方法,两处都有数据
$.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 方法,数据在地址后面, 第二个参数为回调函数
$.post('jquery_data.php?v1=1', function(data)
{
$('<pre/>').append(data).appendTo('body');
});
});

Return the result, the data is in get:

post: Array
(
)
get: Array
(
[v1] => 1
)

Experiment 3:

$(function()
{
// get 方法,用 data 参数传值
$.get('jquery_data.php', {v2: 2}, function(data)
{
$('<pre/>').append(data).appendTo('body');
});
});

Return the result, the data is in get:

post: Array
(
)
get: Array
(
[v2] => 2
)

Experiment 4:

$(function()
{
// get 方法,两处都有数据
$.get('jquery_data.php&#63;v1=1', {v2: 2}, function(data)
{
$('<pre/>').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 方法,两处都有数据,且变量名相同
$.get('jquery_data.php&#63;v2=1', {v2: 2}, function(data)
{
$('<pre/>').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
)

Through these simple examples, it is not difficult to see that the data behind the address is always transferred 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 transferred according to 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