JQuery AJAX get () and post () methods

Source: Internet
Author: User
Tags http post php script

The JQuery get () and post () methods are used to request data from the server over an HTTP get or POST request.

HTTP Request: GET vs. POST

Two common methods of requesting-responding on the client and server side are GET and POST.

    • GET -Request data from the specified resource
    • POST -submits the data to be processed to the specified resource

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

POST can also be used to fetch data from the server. However, the POST method does not cache data and is often used to send data together with the request.

To learn more about get and POST and the differences between the two methods, please read our HTTP Method-GET vs. Post.

JQuery $.get () method

The $.get () method requests data from the server over an HTTP GET request.

Syntax: $.get ( URL, Callback);

The required URL parameter specifies the URL that you want to request.

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

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

Instance $ ("button"). Click (function () {
$.get ("demo_test.html", function (data,status) {
Alert ("Data:" + Data + "Nstatus:" + status);
});
});
Try it»

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

The second parameter is a callback function. The first callback parameter contains the contents of the requested page, and the second callback parameter holds the requested State.

tip: This PHP file ("demo_test.php") looks like this:

<?php
echo "This was some text from an external PHP file.";
?>

JQuery $.post () method

The $.post () method requests data from the server over an HTTP POST request.

Grammar:

$.post ( Url,data,callback);

The required URL parameter specifies the URL that you want to request.

The optional data parameter specifies the information to be sent along with the request.

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

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

Instance $ ("button"). Click (function () {
$.post ("demo_test_post.html",
{
Name: "Donald Duck",
City: "Duckburg"
},
function (data,status) {
Alert ("Data:" + Data + "Nstatus:" + status);
});
});
Try it»

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

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

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

The third parameter is a callback function. The first callback parameter holds the contents of the requested page, while the second parameter holds the requested State.

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

<?php
$name = isset ($_post[' name ')? Htmlspecialchars ($_post[' name '): ';
$city = isset ($_post[' city ')? Htmlspecialchars ($_post[' city '): ';
Echo ' Dear '. $name;
Echo ' Hope live Well '. $city;
?>

JQuery AJAX get () and post () methods

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.