jQuery post() method
Example 1
Load data from the server using an HTTP POST request:
$
(
"
button
"
)
.
click
(
function
(
)
{
$.
post
(
"
demo_test.html
"
,
function
(
data
,
status
)
{
alert
(
"
Data:
"
data
"
nStatus:
"
status
)
;
}
)
;
}
)
;
Try it »
Example 2
Change using an AJAX POST request
element's text:
data - Contains the resulting data from the request
status - Contains the status of the request ("success", "notmodified", "error", "timeout", "parsererror")
xhr - Contains the XMLHttpRequest object
| | *dataType* | Optional. Specifies the expected data type of the server response. By default, jQuery makes an intelligent guess. Possible types:
$
(
"
input
"
)
.
keyup
(
function
(
)
{
txt
=$
(
"
input
"
)
.
val
(
)
; $.
post
(
"
demo_ajax_gethint.html
"
,
{
suggest
:
txt
}
,
function
(
result
)
{
$
(
"
span
"
)
.
html
(
result
)
;
}
)
;
}
)
;
Try it »
Definition and Usage
The $.post() method loads data from the server using an HTTP POST request.
Syntax
$(
selector
).post(
URL,data,function(data,status,xhr),dataType)
| Parameters | Description |
|---|---|
| URL | Required. Specifies the URL to which the request is sent. |
| data | Optional. Specifies the data to send to the server along with the request. |
| function(data,status,xhr) | Optional. Specifies the function to run when the request succeeds. |
额外的参数:
- "xml" - An XML document
- "html" - HTML as plain text
- "text" - A plain text string
- "script" - Runs the response as JavaScript and returns it as plain text
- "json" - Runs the response as JSON and returns it as a JavaScript object
- "jsonp" - Loads a JSON block using JSONP, adding a "?callback=?" to the URL to specify the callback
jQuery AJAX methods