


Value-passing testing and precautions for get and post methods in jQuery_jquery
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?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?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.

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 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

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

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

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

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
