Test the get and post methods for passing values and precautions in jQuery, jqueryget

Source: Internet
Author: User

Test the get and post methods for passing values and precautions 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, use 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 and 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.


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

How to pass jquery post values

JQuery. post (url, [data], [callback], [type])
Parameters:
Url: The sending request address.

Data: Key/value parameter to be sent.

Callback: callback function when sending successfully.

Type: The returned content format is xml, html, script, json, text, and _ default.
Example:
$. Post ("test. php", {"func": "getNameAndTime"}, function (data) {// after successful POST, execute this function. DATA is the returned data. });

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.