jquery Ajax Post instance and detailed description

Source: Internet
Author: User
Tags error code php tutorial

Jquery.post (URL, [data], [Success (data, Textstatus, XMLHttpRequest)], [datatype])
Returns:xmlhttprequest

URL A string that contains the URL to which the request is sent

Data map or string, send request to server

Success (data, Textstatus, XMLHttpRequest) A callback function is executed if the request succeeds

DataType the expected data type of server

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

The data pass that is returned successfully by the callback function, which is determined by the MIME type of an XML root element or a text string response. It also passes the response text state.

In jquery 1.4, the successful callback function is also passed through the XMLHttpRequest object.

Most implementations will specify a successful handler:

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

This example gets the HTML fragment on the requested page and inserts it.

The forced page and post are not cached, so the caching and Jquery.ajaxsetup ifmodified options () have no effect on these requirements.

Additional considerations:
Due to browser security restrictions, most "Ajax" requirements are based on the same Origin policy; Request cannot successfully retrieve data from a different domain, subdomain or protocol.
If a jquery.post () request returns an error code, it will silently fail unless the script also calls the global. Ajaxerror () method.
Example:
For example: a Web page that requests a test.php tutorial, but ignores the return result.

$.post ("test.php");

For example, request a test.php page and follow some extra data (while ignoring the return result).

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

For example, an array of data is passed to the server (and the return result is ignored).

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

For example: Send form data using AJAX requests

$.post ("test.php", $ ("#testform"). Serialize ());
For example, alert is the result of the request test.php (HTML or XML, and what is returned).


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

For example: Alert is the result of an additional payload test.php from the same data request (HTML or XML, on what is returned).

$.post ("test.php", {name: "John", Time: "2pm"},
function (data) {
Alert ("Data loaded:" + data);
});

For example, get test.php's Web page content, store it in a Xmlhttpresponse object, and apply it to the process () Web effects function.

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

For example, the page that gets test.php is already in format ()

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

An instance of an application

$.ajax ({
Type: "Post",
URL: ".. /das ",
ContentType: "Text/json",
DataType: ' JSON ',
Data: $.tojson ($ ("form[name=dasform]"). Serializearray ()),
Success:function (msg) {}
);

Related Article

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.