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

  • 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 be added to 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 Methodspage.

jQuery $ .get() method

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

This is$.get()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) Geben Sie die Callback-Funktion an, die bei Erfolg des Anrufs ausgeführt wird

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

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

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()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) Geben Sie die Callback-Funktion an, die bei Erfolg des Anrufs ausgeführt wird

Dieser Beispielaufruf fragt die Seite ajax_post.php ab, sendet einige andere Daten und gibt eine Statusmeldung als Warnung aus:

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

Dies ist der Quellcode der PHP-Datei ('ajax_post.php'):

<?php
    echo "<p>Hello " . $_POST['fname'] . " " . $_POST['lname'] . ", Wie geht es dir?";/p>";
?>

jQuery AJAX Referenz

Für eine vollständige Referenz der AJAX Methoden besuchen Sie bitte unserejQuery AJAX Referenz.