Notes on passing values in jQuery get and post methods_jquery
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
echo "post: ";
print_r($_POST);
echo "get: " ;
print_r($_GET);
?>
jquery_test.html
Experiment 1:
$(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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Title: PHP code example: How to use POST to pass parameters and implement page jumps In web development, it often involves the need to pass parameters through POST and process them on the server side to implement page jumps. PHP, as a popular server-side scripting language, provides a wealth of functions and syntax to achieve this purpose. The following will introduce how to use PHP to implement this function through a practical example. First, we need to prepare two pages, one to receive POST requests and process parameters

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

In Linux, URL or Curl client is a popular command line utility that allows you to transfer data over the network using various protocols such as HTTPS, HTTP, FTP, etc. It allows you to send and receive data using its get, post and request methods. Among them, you need to use the "get" method frequently. Therefore, it becomes crucial to learn various methods and various options that you can use to increase your productivity. "Performing a curl operation is as simple as entering a few simple commands. Although it seems simple, many users do not fully realize its potential. Therefore, this short guide provides some information on how to perform curl operations on Linux systems. Example using the "curlget" command." Curl
