$.ajax () method parameters in jquery and case studies

Source: Internet
Author: User
Tags script tag browser cache

Load remote data over HTTP requests. JQuery underlying AJAX implementations. Easy to use high-level implementation see $.get, $.post and so on. $.ajax () returns the XMLHttpRequest object that it created. In most cases you do not need to manipulate the function directly unless you need to manipulate the infrequently used options for more flexibility. In the simplest case, $.ajax () can be used directly without any parameters. Note, all options can be set globally through the $.ajaxsetup () function.

By default, AJAX requests use the Get method. If you want to use the Post method, you can set the type parameter value. This option also affects how content in the data option is sent to the server.

URL: The requested address is requested for a parameter of type string, (the current page address is assumed to be the default).

Type: A parameter of type string is required, and the request method (post or get) defaults to get. Note Other HTTP request methods, such as put and

Delete can also be used, but only some browsers support it.

Timeout: Requires a parameter of type number to set the request time-out (in milliseconds). This setting overrides the global setting of the $.ajaxsetup () method

set .

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 sync request will lock the browser, and the user's other actions must wait

The request is complete before it can be executed.

Cache: A parameter that is required to be of type Boolean, which is true by default (False when datatype is a script).

set to false to not load the request information from the browser cache.

Data: A parameter that is required to be an object or string that is sent to the server. Automatically converts to a string if it is not already a string

type. The GET request will be appended to the URL. To prevent this automatic conversion, you can view the ProcessData option. The object must be a key/value grid

For example {foo1: "Bar1", Foo2: "Bar2"} converted to &foo1=bar1&foo2 =bar2. If it is an array, jquery will automatically be a different

The value corresponds to the same name. For example {foo:["Bar1", "Bar2"]} is converted to &FOO=BAR1&FOO=BAR2.

DataType: A parameter of type string is required and the expected data type is returned by the server. If not specified, jquery is automatically mime based on the HTTP package

information returned to Responsexml or ResponseText, And passed as a callback function parameter.

The following types are available:

xml: Returns an XML document, Available with jquery.

HTML: Return plain text htm l information; The included script tag is executed when the DOM is inserted.

script: Return plain text J Avascript code. Results are not automatically cached. Unless the cache parameter is set. Note in the remote request

            (not under the same domain), all post requests are converted to get requests.

JSON: Returning JSON data 。

jsonp:jsonp format 。 When a function is called using the Sonp form, for example Myurl?callback=?,jquery will automatically replace the latter

          < c45> " ? " is the correct function name to execute the callback function.

text: Returns a plain text string 。

Beforesend: A function that requires a parameter of type function that can modify a XMLHttpRequest object before sending a request, such as adding a custom

http headers. If you return False in Beforesend, you can cancel this Ajax request. The XMLHttpRequest object is the only parameter

          < number of c21>.

          < c32> Function (XMLHttpRequest) {

          <  c43> this ; //The options parameter passed when calling this Ajax request

          < c60> }

Complete: A parameter that is required to be a function type, called when the request is completed (invoked when the request succeeds or fails).

Parameters: The XMLHttpRequest object and a description of the successful request type String.

function ( XMLHttpRequest, Textstatus) {

          < c28> this; //The options parameter passed when calling this Ajax Request

         }

Success: A callback function called after a successful request for a parameter of function type, with two parameters.

( 1 ) returned by the server, and the processed data according to the datatype parameter.

( 2 ) A string describing the status.

function ( data, Textstatus ) {

          < /c18> //data may be xmldoc, jsonobj, HTML, text, etc.

          < c30> this; //The options parameter passed when calling this Ajax request

Error: A function that requires a parameter of type function, which is called when the request fails. The function has 3 parameters, that is, the XMLHttpRequest object, the wrong

error message , and optionally, the wrong object to be captured.

the Ajax event functions are as follows:

function ( xmlhttprequest, textstatus, Errorthrown) {

//normally textstatus and Errorthrown only One of them contains information

this; //The options parameter passed when calling this Ajax request

      }

ContentType: Requires a parameter of type string, when sending information to the server, the content encoding type is default

          < The c36> is "application/x-www-form-urlencoded". This default value is suitable for most applications.

Datafilter: A function that requires the preprocessing of the original data returned by Ajax as a parameter of the function type.

          < c48> provides data and type two parameters. Data is the original information returned by Ajax, type is provided when calling Jquery.ajax

datatype parameters 。 The value returned by the function will be further processed by jquery.

          < c21> function (data, type) {

          < c32> / / returns the processed data

          < c47> return data ;

          < c62> }

Global: A parameter that is required to be of type Boolean, which is true by default. Indicates whether global AJAX events are triggered. Set to False will not trigger the global

Ajax Events , Ajaxstart or ajaxstop, can be used to control various AJAX events.

Ifmodified: A parameter of type Boolean is required, and the default is False. Get new data only when the server data changes.

           Server data change judgment is based on last-modified header information. The default value is False, which ignores header information.

Jsonp: Requires a String type parameter 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 the GET or POST request, such as

{ jsonp : ' Onjsonpload '} will cause the "onjsonpload=?" passed to the server.

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

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

ProcessData: A parameter that is required to be of type Boolean, which is true by default. By default, the sent data is converted to an object (from a technical perspective

           

          < c22> Tree Information or other information you do not want to convert, set to False.

Scriptcharset: A parameter that is required to be of type string, only if the request is datatype as "JSONP" or "script" and the type is get

          < c34> is used to force the character set (charset) to be modified. Typically used when local and remote content encodings are different.

ExampleDescription: Loads and executes a JS file. JQuery Code: $.ajax ({type: "GET", url: "Test.js", DataType: "Script"}); Description: Saves the data to the server and displays the information when it succeeds. JQuery Code: $.ajax ({type: "POST", url: "some.php", Data: "Name=john&location=boston", Success:function (msg) {alert (" Data Saved: "+ msg);}}); Description: Loads the latest version of an HTML page. JQuery Code: $.ajax ({url: "test.html", Cache:false, Success:function (HTML) {$ ("#results"). Append (HTML);}); Description: Load data synchronously. Lock the browser while sending the request. Use synchronization when you need to lock down user interactions. JQuery code: var html = $.ajax ({url: "some.php", Async:false}). responsetext; Description: Sends XML data to the server. Set the ProcessData option to False to prevent the automatic conversion of data formats. JQuery code: var xmlDocument = [create XML document]; $.ajax ({url: "page.php", Processdata:false, Data:xmldocument, success:handleresponse}), JQuery Post Instance code: $.post ("Test . php ", {name:name,pwd:pwd},function (msg) {alert (msg);}) The test.php inside uses $_post["name" and $_post["PWD"] to receive parameters. $.post The first parameter is url,{} here is the parameter name and value between the use: non-open, multiple values with, separate, the last is the function of the return MSG is the result of the return. Return the unused data according to your needs. $.get and Post, PHP received the words changed to $_get["name" and $_get["pwd"]

Case code (user login verification):

<script type= "Text/javascript" >$ (document). Ready (function () {$ ("#inputs1"). Click (function () {var account = $ (" #uname "). Val (), var pass = $ (" #upass "). Val (); $ (" #divPass "). HTML (" "); $.ajax ({url:"/office/user/loginaction ", type:" Post ", Data: {account:account,pass:pass},success:function (data) {if (data==" yes ") {location.href="/office/user/ Mainindexmenu ";} else{$ ("#divPass"). HTML ("User name or password error"),}});}); </script>



$.ajax () method parameters in jquery and case studies

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.