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

jQuery post() Method

jQuery AJAX Methods

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

Syntax:

$.post(URL, data, callback, dataType)

Instance

This example retrieves the requested HTML code segment and inserts it into the page:

$("button").click(function(){
  $.post("ajax_post.php", function(data){
    $("#output").html(data);
  });
});
Test and see‹/›

Request the ajax_post.php page and send some other data:

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

Request the ajax_post.php page, send some other data, and display 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‹/›

Request the demo.json file and insert it into the page:

$("button").click(function(){
  $.post("demo.json", function(data){
    let myObj = JSON.parse(data);
    $("#output").html(myObj.name);
  });
});
Test and see‹/›

Request json_demo1.php file, which has been returned in json format:

$("button").click(function(){
  $.post("json_demo1.php", function(data){
    let myObj = JSON.parse(data);
    $("#output").html(myObj.name);
  });
});
Test and see‹/›

Parameter value

ParameterDescription
URLSpecify 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 the callback function to be executed after the request is successful

Parameters:

  • data-Contains the result data from the request

  • status-Contains the status of the request ("success", "notmodified", "error", "timeout", or "parsererror")

  • xhr-Contains the XMLHttpRequest object

dataType(Optional) Specify the data type required by the server response
By default, jQuery performs automatic guessing

Possible types:

  • “xml”-An XML document

  • “html”-HTML as plain text

  • “text”-Plain text string

  • “script”-Run the response as JavaScript and return it as plain text

  • “json”-Run the response as JSON and return a JavaScript object

  • “jsonp”-Use JSONP to load JSON blocks. Add a "?callback=?" URL to specify the callback

jQuery AJAX Methods