Ajax-based on the introduction of jquery

Source: Internet
Author: User


Ajax Basics Supplement

The XMLHttpRequest object has the following properties and actions
Property:
onReadyStateChange
ReadyState
Status
Operation:

jquery's AJAX operations, commonly used functions are as follows:

$.ajax ([options])//Bottom-level Ajax method
$.get (URL, [data], [FN], [type])//get-based Ajax
$.post (URL, [data], [FN], [type])//post-based Ajax
Load (URL, [data], [callback])//This is the method that the object calls (above is the global method), can be a GET or post


Explanation of the above 4 function-related parameters:

URL: Request Path
Data: submitted to the server
Fb/cllback: Callback function with three parameters function (resp, textstatus, xmlHttp), equivalent to Xmlhttp.onreadystatechenge = function () {}
Data: returned by the server
Info: The status returned
Xmlhttp:ajax Engine Object
Note: The rollback function in Get and post only executes if the request succeeds
Type: The data type returned
Parameter collection in Options:json format



The functions are described individually:

$.ajax ([options]) method
The method parameter is a JSON object, and the parameters in the object are stored as key-value pairs, the common parameters are as follows:

Async: The default is true, which means asynchronous and set to false to indicate request synchronization (the Ajax form checksum given to jquery validate needs to be set to synchronize)
Data: Sent to the server is automatically converted to the request string format, if get is also automatically added to the URL after
DataType: The expected data type returned by the server, if not specified, is automatically determined based on the MIME type (such as Application/json) returned in the server
Type: Submit method, Get or post
URL: Request Address
Success: callback function after successful request, formatted as function (RESP, textstatus, XmlHttp)
Error: callback function after request failed, formatted as function (XmlHttp, textstatus, exception)
ContentType: The type of data sent by the server, when it is used as the JSON string


$.get (URL, [data], [FN], [type]) method
Get Submit Method
The callback function is formatted as: function (resp, textstatus, XmlHttp)

$.post (URL, [data], [FN], [type]) method
Same as the Get commit method, except that the commit is post

Load (URL, [data], [callback])

The callback function is formatted as: function (resp, textstatus, XmlHttp)

This method is a method that is called by the only object in all Ajax action methods, and the others are global methods
If a request parameter (JSON data format or Key/value string) is present at the time of submission, that is, the data part exists as a post submission, and the data part does not exist that is a get commit
The format for converting the JSON-formatted string returned by the server to a JSON-formatted object is eval ("(" +json+ ")");
The reason: eval itself is a problem. Since JSON is started and ended in the way "{}", in JS, it is treated as a block of statements,
So it has to be forced to convert it into an expression, so add extra ()

Form serialization issues

If you want the form to be submitted asynchronously via Ajax, first we will get the value entered in each form via JS,
If the form item is more, it must be a very troublesome and painful thing,
In this case, we can stitch the form's data into the submitted parameter format via jquery's form serialization operation
That is: Name=value&name=value&name=value or JSON Format object

var res = $ ("Selector for the form"). Serialize (); Request for a form can be obtained


Ajax-based on the introduction of jquery

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.