Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery's post request


May 31, 2021 Article blog


Table of contents


Get and post request overview

# jQuery # In HTTP, There Are Many Ways To Request, Such AS Get, POST, ETC., But The Most Commonly Used Is To Get Data And Submit Data, Get Data IS Get Request, CommT Data IS Post Request.

Note: HTTP requests are requests sent by clients to the server. For example, use your computer to open a browser, enter the "Baidu" URL, this is from your computer to Baidu's server to initiate HTTP requests.

This article is dedicated to the $.post method

The $.post ( ) method

The $.post() method allows the contents of html clients to be submitted to the server, and data from Post can also be stored in the database.

Note that the query string (name/value pair) was sent in the HTTP message body of the POST request:

POST /test/demo_form.php HTTP/1.1

Host: w3cschool.com

name1=value1&name2=value2

Some other comments about POST requests:

  • POST requests are not cached
  • POST requests are not retained in the browser history
  • POST cannot be bookmarked
  • POST requests do not require data length

The $.post() method submits data to the server through an HTTP POST request.

grammar:

$.post( URL,data,callback );

The required URL parameters specify the URL that you want to request.

The optional data parameter specifies that the data is sent together with the request.

The optional callback parameter is the name of the function executed after the request succeeds.

The following example uses $.post() to send data along with the request:

$("button").click(function(){
$.get("demo_test.php",function(data,status){
Alert ("Data: + Data +" \ n status: "+ status);
});
});


Give it a try . . .

The first argument for $.post() is the URL we want to request ("demo_test_post.php").

We then send the data along with the request (name and url).

The PHP script in "demo_test_post.php" reads these parameters, processes them, and returns the results.

The third argument is the callback function. The first callback parameter has the contents of the requested page, while the second parameter has the status of the request.

Tip: This PHP file ("demo_test_post.php") looks something like this:

<?php

$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';

$url = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : '';

Echo 'website name:'. $ name;

echo "\n";

Echo 'URL Address:'. $ URL;

?>


The difference between the get method and the post method

function GET POST
Back button/refresh harmless The data is resubmitted (the browser should inform the user that the data will be resubmitted).
bookmark Can be collected as bookmarks Cannot be bookmarked
cache can be cached Cannot be cached
The type of encoding application/x-www-form-urlencoded application/x-www-form-urlencoded or multipart/form-data。 Use multiple encoding for binary data.
history The parameters remain in the browser history. Arguments are not saved in browser history.
Limits on the length of the data That's right. When data is sent, the GET method adds data to the URL; Unlimited.
Restrictions on data types Only ASCII characters are allowed. There are no restrictions. Binary data is also allowed.
security GET is less secure than POST because the data sent is part of the URL.

Never use GET! when sending passwords or other sensitive information
POST is more secure than GET because parameters are not saved in browser history or web server logs.
Visibility The data is visible to everyone in the URL. The data does not appear in the URL.