Latest web development tutorials

jQuery - AJAX get () and post () method

jQuery get () and post () method is used to request data from the server via HTTP GET or POST request.


HTTP request: GET vs. POST

Two kinds of client and server-side request - common method of response is: GET and POST.

  • GET - request data from a specified resource
  • POST - submission of data to be processed to the designated resources

GET is basically used to obtain (retrieve) data from the server. Note: GET method may return cached data.

POST can also be used to retrieve data from the server. However, POST method does not cache data, and is commonly used in conjunction with a request to send data together.

To learn more about GET and POST methods and differences in two knowledge, please read our HTTP method - GET contrast POST .


jQuery $ .get () method

$ .get () Method to request data from the server via HTTP GET requests.

grammar:

$.get(URL,callback);

URLparameters necessary for a predetermined URL you want to request.

The optionalcallbackparameter is the name of the function executed after the request is successful.

The following example uses $ .get () method to retrieve data from a file on the server:

Examples

$("button").click(function(){
$.get("demo_test.php",function(data,status){
alert("数据: " + data + "\n状态: " + status);
});
});

try it"

$ .get () The first parameter is the URL we want to request ( "demo_test.php").

The second parameter is a callback function. The first callback parameter content of the page there is a request, there are second callback parameter status of the request.

Tip: PHP file ( "demo_test.php") like this:

<?php
echo '这是个从PHP文件中读取的数据。';
?>


jQuery $ .post () method

$ .post () Method to request data from the server via HTTP POST requests.

grammar:

$.post(URL,data,callback);

URLparameters necessary for a predetermined URL you want to request.

The optionaldataparameter specifies the data sent along with the request.

The optionalcallbackparameter is the name of the function executed after the request is successful.

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

Examples

$ ( "Button"). Click (function () {
$ .post ( "/ Try / ajax / demo_test_post.php",
{
name: "tutorial"
url: "http://www.w3big.com"
},
function (data, status) {
alert ( "data: \ n" + data + "\ n Status:" + status);
});
});

try it"

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

Then we together with the request (name and city) to send data together.

"Demo_test_post.php" The PHP script reads these parameters, process them, and then returns the result.

The third parameter is the callback function. The first callback parameter there requested page content, and the second argument there status of the request.

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

<?php
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
$city = isset($_POST['url']) ? htmlspecialchars($_POST['url']) : '';
echo '网站名: ' . $name;
echo "\n";
echo 'URL 地址: ' .$city;
?>