ドキュメント

Ajax/jQuery.post

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Ajax

jQuery.post( url, [data][callback][type] )

Load a remote page using an HTTP POST request.
This is an easy way to send a simple POST request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code).

The returned data format can be specified by the fourth parameter. If you need to have both error and success callbacks, you may want to use $.ajax. $.post is a (simplified) wrapper function for $.ajax.

$.post() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually.
Arguments:


urlString
The URL of the page to load.
data (Optional)Map, String
Key/value pairs or the return value of the .serialize() function that will be sent to the server.
callback (Optional)Function
A function to be executed whenever the data is loaded successfully.
function (data, textStatus) {
  // data could be xmlDoc, jsonObj, html, text, etc...
  this; // the options for this ajax request
  // textStatus can be one of:
  //   "timeout"
  //   "error"
  //   "notmodified"
  //   "success"
  //   "parsererror" 
  // NOTE: Apparently, only "success" is returned when you make
  // an Ajax call in this way. Other errors silently fail.
  // See above note about using $.ajax.
}
type (Optional)String
Type of data to be returned to callback function: "xml", "html", "script", "json", "jsonp", or "text".
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};
Examples:

Request the test.php page, but ignore the return results.

$.post("test.php");

Request the test.php page and send some additional data along (while still ignoring the return results).

$.post("test.php", { name: "John", time: "2pm" } );

pass arrays of data to the server (while still ignoring the return results).

$.post("test.php", { 'choices[]': ["Jon", "Susan"] });

send form data using ajax requests

$.post("test.php", $("#testform").serialize());

Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.post("test.php", function(data){
  alert("Data Loaded: " + data);
});

Alert out the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

Gets the test.php page content, store it in a XMLHttpResponse object and applies the process() JavaScript function.

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    process(data);
  }, "xml");

NameType
Gets the test.php page contents which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>)

$.post("test.php", { func: "getNameAndTime" },
  function(data){
    alert(data.name); // John
    console.log(data.time); //  2pm
  }, "json");