How to use the JQuery post () event

Source: Internet
Author: User
Tags http post json parse error

Loading data through a server HTTP POST request

Version added:1.0jquery.post (URL, [data], [Success (data, Textstatus, JQXHR)], [DataType])
URL a URL string containing the request sent

Data sends the requested object or string parameter to the server

Success (data, Textstatus, JQXHR) A callback function that executes when a request succeeds.

DataType the expected data type returned from the server. Default: Intelligent guessing (XML, JSON, script, or HTML).

This is a fast AJAX processing function, which is equivalent to:

The code is as follows Copy Code

$.ajax ({
Type: ' POST ',
Url:url,
Data:data,
Success:success
Datatype:datatype
});

The success callback function is based on the MIME type response, which includes the XML root node, string, JavaScript file, or JSON object by the returned data. It also passes the response text state.

In jquery 1.5, the success callback function also passes the "JQXHR" object (in jquery 1.4, it is through the XMLHttpRequest object).

Most implementations will specify a successful handler function:

The code is as follows Copy Code

$.post (' ajax/test.html ', function (data) {
$ ('. Result '). HTML (data);
});

This example inserts all the HTML code fragments that are requested in the page.

Pages obtained by POST are never cached, so the cache and ifmodified options in these requests are not effective in jquery.ajaxsetup ().


This function is shorthand for the following function

The code is as follows Copy Code
$.ajax ({
Type: ' POST ',
Url:url,
Data:data,
Success:success,
Datatype:datatype
});

Use examples:

The code is as follows Copy Code

$.post ("test.php", {"Func": "Getnameandtime"},
function (data) {
Console.log (Data.name); John
Console.log (Data.time); 2pm
}, "JSON")
. Success (function () {alert ("second Success");})
. Error (function () {alert ("error");})
. Complete (function () {alert ("complete");});

A detailed description of the datatype parameter

The data type expected to be returned from the server. If not specified, JQuery will be based on the MIME type of the return result (XML MIME type will produce XML, in 1.4 JSON will produce a JavaScript object, in 1.4 script will execute the script, and others will act as a string Returns) to attempt to infer it. The available types (and the final result of the first argument passed to the success callback function) are:

"XML": Returns an XML document that can be parsed through jquery.

"HTML": return HTML as plain text; Included script tags are evaluated when inserted in the DOM.

Script: Treats the return value as JavaScript and returns as normal text. Disables caching by appending a query string parameter, ' _=[timestamp] ', to the URL unless the cache option is set to True . Note:this'll turn POSTs into GETs for Remote-domain requests.

"JSON": treats the return value as JSON and returns a JavaScript object. In JQuery 1.4 JSON data is parsed in a rigorous manner; Any malformed JSON is rejected and throws a parse error. (Refer to the json.org Web site for more information about the correct JSON format.)

"JSONP": loads in a JSON block using JSONP. Adds an extra ", callback=?" to the ' end of your ' URL to specify the callback. Disables caching by appending a query string parameter, ' _=[timestamp] ', to the URL unless the cache option is set to True .

' Text ': Plain text string.

Multiple space-delimited values: from jquery 1.5, jquery can convert datatype from Content-type headers to other types you need. For example, if you want to treat a text return value as XML, use "text xml" as the DataType. can also make a JSONP request, have it received as text, and interpreted by JQuery as xml: "JSONP text XML." Similarly, a shorthand string such as "JSONP xml" would-be-to-convert from-attempt to XML, and, JSONP, failing ert from Jsonp to text, and then from the text to XML.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.