$.ajax () Knowledge

Source: Internet
Author: User
Tags script tag browser cache

I. $.ajax ():

1.url:
Requires a parameter of type string to send the requested address.

2.type:
Requires a parameter of type string, the request method (post or get), and the default is get.

3.timeout:
Requires a parameter of type number to set the request time-out (in milliseconds).

4.async:
Requires a parameter of type Boolean, which is set to True by default and all requests are asynchronous requests. If you need to send a synchronization request, set this option to false. Note that the synchronization request locks the browser, and the user's other actions must wait for the request to complete before it can be executed.

5.cache:
A parameter that is required to be of type Boolean, which defaults to True (False when datatype is a script), and set to false will not load the request information from the browser cache.

6.data:
  Requires data to be sent to the server as an object or a string of type parameters. If it is not already a string, it is automatically converted to a string format. The GET request will be appended to the URL. Prevent this kind of automatic conversion, such as {foo1: "Bar1", Foo2: "Bar2"} into &foo1=bar1&foo2=bar2. In the case of arrays, jquery automatically corresponds to the same name for different values. For example {foo:["Bar1", "Bar2"]} is converted to &FOO=BAR1&FOO=BAR2.

7.dataType :
requires a parameter of type string, Expected data type returned by the server. If not specified, jquery automatically returns Responsexml or ResponseText based on the HTTP packet mime information and is passed as a callback function parameter. The following types are available:
xml: Returns an XML document that can be processed with jquery.
html: Returns plain text HTML information, and the included script tag is executed when the DOM is inserted.
script: Returns plain-text JavaScript code. Results are not automatically cached. Unless the cache parameter is set.
json: Returns the JSON data.
JSONP:JSONP format. When a function is called using the Jsonp form, for example Myurl?callback=?,jquery will automatically replace the latter "?" is the correct function name to execute the callback function.
text: Returns a plain-text string.

8.beforeSend:
  Functions executed before sending a request
9.complete:
  The callback function that is called after the request completes (called when the request succeeds or fails). Parameters: The XMLHttpRequest object and a string that describes the successful request type.

10.success:

  callback function called after successful request, there are two parameters.
(1) The data returned by the server and processed according to the datatype parameter.
(2) A string describing the status.

11.error:
  The function that was called when the request failed. The function has 3 parameters, the XMLHttpRequest object, the error message, and optionally the error object being captured. The Ajax event functions are as follows:
function (XMLHttpRequest, textstatus, Errorthrown) {
}

12.contentType:
  A parameter of type string is required, and when the message is sent to the server, the content encoding type defaults to "application/x-www-form-urlencoded". This default value is suitable for most applications.

13.dataFilter:
  A function to preprocess the raw data returned by Ajax. Provides data and type two parameters. Data is the original data returned by Ajax, and type is the datatype parameter that is provided when Jquery.ajax is called. The value returned by the function will be further processed by jquery.

14.jsonp:
  Requires a parameter of type string to override the name of the callback function in a JSONP request. This value is used instead of the "callback=?" The "callback" part of the URL parameter in this get or POST request, such as {jsonp: ' onjsonpload ', causes the "onjsonpload=?" passed to the server.

15.username:
  A parameter of type string that is required to respond to the user name of the HTTP access authentication request.

16.password:
A parameter of type string that is required to respond to the password for HTTP access authentication request.

17.processData:
  Requires a parameter of type Boolean, which is true by default. By default, the data sent 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.

Case code:

$(function() {$ (' #send '). Click (function() {$.ajax ({type: "GET", url: "Test.json", 
timeout:10000, success: Span style= "COLOR: #0000ff" >function// empty all contents of Restext var html =" ; $.each (data, function (Commentindex, comment) {html + = ' <div class= "comment" >] + ': "+ ' </p></div> ' ; }); $ (' #resText '

Two. $.get ():

  $.get (Url,[data],[callback])

Description: URL is the request address, data is the list of request data, callback is the callback function after the successful request, the function accepts two parameters, the first is the data returned by the server, the second parameter is the state of the server, is an optional parameter.

The format of the data returned by the server is actually in the form of a string, not the JSON data format we want;

$.get (' login.php ',
{ID: ' Robin ', Password: ' 123456 ', Gate: ' Index '
function (data, status) {//data is the returned object, status is the requested State
var content=eval (' (' +data+ ') '); alert (data);
alert (status); The result is success, error, and so on, but this is the function that will run when it succeeds});

Three. $.post ():

$.post (Url,[data],[callback],[type])

Description: This function is similar to the $.get () parameter, has a type parameter, type is the requested data type, can be a type of Html,xml,json, if we set this parameter to: JSON, then the returned format is in JSON format, if not set, and $. Get () returns the same format as the string

  
$.post ("data.php", {a: "111", B: "222"},function (data) {        $ ("#postResponse"). HTML (data.name);    }, "JSON"// Set the type of get data, so the resulting data format is JSON type);    

  

$.ajax () Knowledge

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.