"Learning Notes" sharp jquery (iv) AJAX

Source: Internet
Author: User
Tags getscript

One, load () method

/** If there is no parameter pass, the GET method is passed * If there are parameters, it is automatically converted to post mode pass * Regardless of whether Ajax request succeeds, the callback function triggers after the request completes*/Load ("test.html") Load ("test.html. Para")//loading the DOM with the Para class in the HTMLLoad"test.php", {name:"Rain", Age:" A"},function (responsetext,textstatus,xmlhttprequest) {//ResponseText: Request What is returned//textstatus: Request Status: success/error/notmodified/timeout 4 kinds//Xmlhttprequest:xmlhttprequest Object})

Ii. $.get () and $.post () methods

The difference between $.get () and $.post ():

1,get requests that parameters be passed after the URL, and post is sent to the server as the entity content of the HTTP message .

2,get has a size limit on the data transmitted, usually no more than 2kb,post.

The data requested by the 3,get method is cached by the browser, the data is leaked, and post avoids such security issues.

/**get and Post are both structured and used the same way * The callback function is called only if the data of the AJAX request is returned successfully*/$.Get("test.php", {name:"Rain", Age:" A"},function (data,textstatus) {//get method parameter can also be string //data: xml/json/html, etc.//textstatus: Request Status: success/error/notmodified/timeout 4 kinds},"JSON")//If the expected return data is in JSON format,set the parameter to "JSON"

Iii. $.getscript () and $.getjson () methods

$.getscript ("Jq.color.js", FN)//when JS is finished loading, execute the callback function//cross-domain JSON data can be loaded by using JSONP in Getjson$.getjson ("Test.json", function (data) {//Data : The JSON data returned//$.each () for traversing objects and arrays, return false to exit each loop$.each (data,function (i,item) {//the index value of an object member or array in the I:data//item:data the corresponding variable or content   }) })

Iv. Universal Ajax Method: $.ajax ()

$.ajax ({type:"GET",//Request mode Get/post, default getUrl:"Test.json",//send the requested addressDataType:"JSON",//expected data types returned by the server   Global:true,//whether to trigger global AJAX events, default to True (trigger)Beforesend:function (XMLHttpRequest) {},//before sending a requestComplete:function (xmlhttprequest,textstatus) {}//when the request is complete (success or failure)Success:function (data,textstatus) {//callback function after the request succeeds$.each (DATA,FN); }, Error:function (Xmlhttprequest,textstatus,errorthrown)//when the request fails})

V. AJAX global Events in jquery

//FN refers to the callback function (callback) Ajaxstart (FN) ajaxstop (FN) ajaxcomplete (FN) ajaxerror (FN) ajaxsend (FN) ajaxsuccess (FN)//If you want an AJAX request to be unaffected by the global way  $.ajax ({   global:false;})  

Vi. methods of serialization elements

//serialized to a stringSerialize () $ ("#form"). Serialize ()//parameters for the entire form$(": Checkbox,:radio"). Serialize ()//the selected parameter//serializing data in JSON formatSerializearray () $ ("#form"). Serializearray () $ (": Checkbox,:radio"). Serializearray ()//serialization of an array or object$.param ()varobj = {a:1B:2C:3};$.param (obj); //The result is a=1&b=2&c=3

"Learning Notes" sharp jquery (iv) AJAX

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.