English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery - AJAX get() and post() methods

jQuery $.get() and $.post() methods are used to request data from the server via HTTP GET and POST requests

HTTP Requests: GET vs POST

inGETIn the method, the browser will name/value pairs are added to the end of the URL

GET is usually used in places where security is not a concern

  • GET requests can be cached

  • GET requests are retained in the browser history

  • GET requests can add bookmarks

  • Never use GET requests when handling sensitive data

  • GET requests have a length limit (only2048characters)

inPOSTIn the method, the content will not be displayed in the URL

Always use POST if the form data contains sensitive information or personal information

  • POST requests will never be cached

  • POST requests will not be retained in the browser history

  • POST requests cannot add bookmarks

  • Use POST requests when handling sensitive data

  • POST requests have no limit on data length

For more information about GET and POST and the differences between the two methods, please visit ourHTTP Request Methodpage.

jQuery $.get() method

jQuery $.get()method uses HTTP GET request to load data from the server

This is$.get()The syntax of method:

$.get(URL, data, callback)

Parameters:

  • URL-Specify the URL you want to request

  • data -(Optional) Specify a pure object or string to be sent to the server along with the request

  • callback-(Optional) Specify a callback function to be executed upon request success

This example requests the page 'ajax_get.php', sends some other data, and issues an alert status message:

$("button").click(function(){
  $.get("ajax_get.php", {fname:"Seagull", lname:"Anna"}, function(data, status){
    $("#output").html(data);
    alert(status);
  });
});
Test and see‹/›

This is the source code of the PHP file ('ajax_get.php'):

<?php
    echo "<p>Hello ". $_GET['fname']." ". $_GET['lname'].", How are u doing?</p>";
?>

jQuery $.post() method

jQuery $.post()method uses HTTP POST request to load data from the server

This is$.post()The syntax of method:

$.post(URL, data, callback)

Parameters:

  • URL-Specify the URL you want to request

  • data -(Optional) Specify a pure object or string to be sent to the server along with the request

  • callback-(Optional) Specify a callback function to be executed upon request success

This example requests the ajax_post.php page, sends some other data, and displays an alert status message:

$("button").click(function(){
  $.post("ajax_post.php", {fname:"Seagull", lname:"Anna"}, function(data, status){
    $("#output").html(data);
    alert(status);
  });
});
Test and see‹/›

This is the source code of the PHP file ('ajax_post.php'):

<?php
    echo "<p>Hello " . $_POST['fname'] . " " . $_POST['lname'] . ", How are u doing?";/p>";
?>

jQuery AJAX Reference

For a complete reference of AJAX methods, please visit ourjQuery AJAX Reference.