This five is a way to get pages or data.
are based on the AJAX protocol.
$.get (Url,[data],[callback])//
Description: loading data from the server, request is get.
URL is request address
Data is a list of the requests
Callback is the callback function after the request succeeds, the function accepts two parameters, the first one is the data returned by the server, the data is in the form of a string ... The second parameter is the state of the server, which is an optional parameter.
Cases:
$.get ("A.ashx", {id:15,ct:new Date ()},function(data) { //data is in string form })
$.post (Url,[data],[callback],[type]) //
Description: load data from server, request in the form of post.
The first three parameters are the same as $.get (Url,[data],[callback]). Focus on the fourth parameter.
Type can specify the types of return values for this request ... The optional values are html,xml,json.
Example:$.post ("A.ashx", {id:15,ct:new Date ()},function(data) { // the type of data becomes the JSON type // You can call the data directly using the Data.id form }, "JSON")
$.load (Url,[data],[callback]) //
Description: loads data from the server and inserts the returned HTML code into the matching element .
Three parameters approximately the previous two methods are the same:
Focus on the URL parameters:
Here the URL can be configured with parameters
Cases:
$ ("#leftNews"). Load ("data.html #section")
The first idea must be to load the contents of the Anchor section in the data.html into this page element:
But it's not. In fact, the part behind this URL is a selector. It's marked by the id=section element.
It means that it is data.html page: The content of the element with the section Id is loaded into the leftnews element on this page:
// executes an asynchronous HTTP (Ajax) request.
What we are talking about today is based on the $.ajax (). So this method can be configured with the most parameters and characteristics are also the most.
Enumerate configuration parameters:
Name of parameter |
Type |
Describe |
wr. |
String |
(Default: Current page address) sends the requested address. |
Type |
String |
(Default: "Get") The Request method ("POST" or "get"), the default is "get". Note: Other HTTP request methods, such as PUT and DELETE, can also be used, but only some browsers support it. |
Timeout |
Number |
Sets the request time-out (in milliseconds). This setting overrides the global settings. |
Async |
Boolean |
(default: TRUE) by default, all requests are asynchronous requests. If you need to send a synchronization request, set this option to false. Note that the sync request will lock the browser, and the user's other actions must wait for the request to complete before it can be executed. |
Beforesend |
Function |
You can modify the functions of the XMLHttpRequest object before sending the request, such as adding a custom HTTP header.
The XMLHttpRequest object is the only parameter. function (XMLHttpRequest) {this;//the "options for this" Ajax request} function (XMLHttpRequest) {this;//the options For this AJAX request}
|
Cache |
Boolean |
(default: TRUE) JQuery 1.2 new feature, set to False will not load the request information from the browser cache. |
Complete |
Function |
The callback function after the request completes (called when the request succeeds or fails).
Parameters: XMLHttpRequest Object, Success information string. function (XMLHttpRequest, textstatus) {this;//the options for this Ajax request} function (XMLHttpRequest, textstatus) {This;//the options for this AJAX request} |
ContentType |
String |
(Default: "application/x-www-form-urlencoded") the content encoding type when sending information to the server. The default values are suitable for most applications. |
Data |
Object, String |
Data sent to the server. is automatically converted to the request string format. The GET request will be appended to the URL. View the ProcessData option description to disallow this automatic conversion. Must be a key/value format. If an array, JQuery automatically corresponds to the same name for the different values. such as {foo:["bar1", "Bar2"]} is converted to ' &foo=bar1&foo=bar2′. |
DataType |
String |
Expected data type returned by the server. If not specified, JQuery will automatically be based on the HTTP packet MIME information Returns Responsexml or ResponseText, and is passed as a callback function parameter, with the available values:"XML": Returns an XML document that can be processed with jQuery. HTML: Returns plain text HTML information, including the script element. "Script": Returns plain text JavaScript code. Results are not automatically cached. "JSON": Returns the JSON data. "JSONP": Jsonp format. When a function is called using the JSONP form, Like "myurl?callback=?" JQuery will be replaced automatically? is the correct function name to execute the callback function. |
Error |
Function |
(Default: This method is called when a request for automatic judgment (XML or HTML) fails.) This method has three parameters: the XMLHttpRequest object, the error message, and (possibly) the error object being captured. function (XMLHttpRequest, textstatus, Errorthrown) {//normally textstatus and Errorthown only one of which has the value of this; Jax Request} function (XMLHttpRequest, textstatus, Errorthrown) {//normally textstatus and Errorthown only one has the value of this;//the Opti ONS for this AJAX request} |
Global |
Boolean |
(default: TRUE) whether to trigger global AJAX events. Setting to False will not trigger a global AJAX event. such as Ajaxstart or Ajaxstop. Can be used to control different AJAX events |
Ifmodified |
Boolean |
(default: false) to get new data only when the server data changes. Use the HTTP packet last-modified header information to determine. |
ProcessData |
Boolean |
(default: TRUE) by default, the sent data is converted to an object (technically not a string) To match the default content type "application/x-www-form-urlencoded". Set to False if you want to send DOM tree information or other information that you do not want to convert. |
Success |
Function |
The callback function after the request succeeds. This method has two parameters: the server returns data, returns the status function (data, textstatus) {//data could be xmldoc, jsonobj, HTML, text, etc ... this;//the options for this Ajax requ EST} function (data, textstatus) {//data could be xmldoc, jsonobj, HTML, text, etc ... this;//the options for this AJA X Request} |
For details, see: Jquery API Chinese Document Jquery.ajax
$.getjson (URL [, data] [, Success (DATA,TEXTSTATUS,JQXHR)]) // loads JSON-encoded data from the server using an HTTP GET request.
The parameters are the same as the above methods: Use up and $.post () the $.get () method is the same: But one thing that needs special attention
$.getjson () The default result is in JSON form: This mechanism is especially suitable for use with the Web API in the background. Use scripting to achieve a variety of excellent user experience UI.
Basically, that's all. Specific what is not the same as to use when appropriate ...
jquery in $.load (), $.get (), $.post (), $.ajax (), $.getjson () with different functions