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.Arguments:
| url | String | |
|---|---|---|
| The URL of the page to load. | ||
| data (Optional) | Map | |
| Key/value pairs 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"
}
| ||
| type (Optional) | String | |
Type of data to be returned to callback function (JSON, XML, etc.)
$.postJSON = function(url, data, callback) {
$.post(url, data, callback, "json");
};
| ||
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");
