Precautions for passing values in get and post methods in jQuery, jqueryget

Source: Internet
Author: User

Precautions for passing values in get and post methods in jQuery, jqueryget

As jQuery knows, jQuery's get and post methods have three parameters: Address, data, and callback functions. But we know that the address can also follow the data (such as get_data.php? V1 = 1 & v2 = 2), and the second parameter can be omitted. That is, the second parameter can be directly written to the callback function, what is the difference between writing data after an address and writing data parameters?

I have just done a few experiments and read the following code:
The following content must be replied before you can see

Jquery_data.php

Echo "post :";
Print_r ($ _ POST );
Echo "get :";
Print_r ($ _ GET );
?>

Jquery_test.html

Lab 1:

$ (Function (){
// Post method, both of which have data
$. Post ('jquery _ data. php? V1 = 1', {v2: 2}, function (data ){
$ ('

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

Returned results:
Post: Array
(
[V2] => 2
)
Get: Array
(
[V1] => 1
)

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

The returned result. 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 returned result. The data is in get:
Post: Array
(
)
Get: Array
(
[V2] => 2
)

Tutorial 4:

$ (Function ()
{
// Get method, both of which have data
$. Get ('jquery _ data. php? V1 = 1', {v2: 2}, function (data)
{
$ ('<Pre/>'). append (data). appendTo ('body ');
});
});

The returned results are merged in the get:
Post: Array
(
)
Get: Array
(
[V1] => 1
[V2] => 2
)

Lab 5:

$ (Function ()
{
// Get method, both of which have data with the same variable name
$. Get ('jquery _ data. php? V2 = 1', {v2: 2}, function (data)
{
$ ('<Pre/>'). append (data). appendTo ('body ');
});
});

The returned result is that the data is in get, and the data in the data parameter overwrites the data after the address:
Post: Array
(
)
Get: Array
(
[V2] => 2
)
From these few simple examples, it is not difficult to see that the data after the address is always transmitted in the form of get, whether the get method or post method is used; the data in the data parameter decides the transmission mode based on the method.

Therefore, to avoid confusion, we recommend that you do not write the data behind the address, but put it in the data parameter.

Of course, if you want to use the post method and get to pass the value, you can write the data to be passed in the get method after the address, write the data to be passed in post mode in the data parameter.

In short, the method is dead, people are active, and how to use it depends on the actual situation. I once said that practice is the only criterion for testing truth. There is nothing to do with the experiment, and the knowledge is better.

How does get and post in JQuery get the value returned by the background?

Returns a json string, for example, ["a", "B", "c"].
Php uses echo to output echo "['A', 'B', 'C']";

Jsp outputs out. print with out. print ("['A', 'B', 'C']" ")

The get and post methods of jq Add the fourth parameter "json", so that the returned json string can directly become a json object.
$. Get ("xxxx. jsp", {}, function (data ){
For (var I = 0; I <data. length; I ++)
{
Alert (data [I])

}

}, "Json ")

The question about Jquery and post methods is, why can't the previous values be output?

You have not figured out the post and load usage. In your example, you only need to select one of the two.

$ I is not output because you have not passed the name to PHP in jQuery.

Use post // name as the key, and the $ _ POST ['name'] in PHP corresponds to // m as the value, $ _ POST ['name'] value // data is returned data $. post ("googleChartUse. php ", {name: m}, function (data) {// place the data in # testShow $ (" # testShow ").html (data );});
Use load
// Load directly places the returned data in # testShow $ ("# testShow"). load ("googleChartUse. php", {name: m });

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.