jquery's support of Ajax

Source: Internet
Author: User
Tags json

  This article describes the support of jquery for Ajax, and interested friends can understand the

1. Three methods     1.1.load methods    //function: Add the data returned by the server directly to the DOM object that meets the requirement  //equivalent to obj.innerhtml = Server returned data   & nbsp Usage:  $obj. Load (url,[request parameters]);    URL: Request address   Request Parameters:  the first form: request strings, such as: ' Username=zs&age=22 ' & nbsp The second form: objects, such as {' username ': ' Zs ', ' age ':22}   //Note: //a, if the load method has no parameters, the request is sent using the Get method. If there are parameters, the request is sent using the Post method.  //b, if there is a Chinese parameter value, the Load method has helped us to encode the processing.     Example:    code is as follows: $ (function () {  $ (' a.s1 '). Toggle (function () {  var airline = $ (this). Parent (). Siblings (). EQ (0). Text ();  $ (a). Next (). Load (' priceinfo.do ', ' airline= ' + airline);  $ (this). HTML (' Show economy class price '); },function () {  $ (this). Next (). Empty ();  $ (this). HTML (' Show All Fares '); }); });       1.2.$.get () and $.post () method    //function: Send a GET or POST request to the server (got request IE cache problem)     usage:  $ . Get (Url,[data],[callback],[type]);  $.post (Url,[data],[callback],[type]);    URL: Request address   Data: Request parameters, form ibid.   CallBack: A callback function that can be used to handle the data returned by the server.   Callback Format:    function (data,statustext),    where data can be obtained from server returns,  StatusText is a simple string that describes the state of the server processing.     Type: The data type returned by the server, which can be:  HTML: Returns HTML content.   Text: Returns the text.   JSON: Returns a JSON string   XML: Returns a DOM-compliant XML object   script: Returns the javascriptz    example:  copy code code as follows: Funct Ion Quoto () {  $.post (' quoto.do ', function (data) { //If the data returned by the server is a JSON string, //is automatically converted to a JS object or an array of JSON objects.   $ (' #tb1 '). Empty ();  for (i=0;i<data.length;i++) {  $ (' #tb1 '). Append (  ' <tr><td> ' + data[i].code  + ' </td><td> ' + data[i].name  + ' </td><td> ' + data[i].price  + ' & Lt;/td></tr> '); } }, ' json ');t }    1.3.$.ajax (options):   // Options are a form like {key1:value1,key2:value2 ...} The JS object that specifies the option to send the request. The     option parameters are:    URL (string)://Request Address   Type (string)://get/post  data (object/string):Data sent to the server   DataType (string)://Expected data type   success (function) returned by the server://callback function called after the success of the request, with two parameters:  function ( Data,textstatus),  where data is the string that the server returns,  Textstatus description state.   Error (function)://functions called when a request fails with three parameters   function (xhr,textstatus,errorthrown),  where XHR is the underlying Ajax object ( XMLHttpRequest),  Textstatus,errorthrown These two parameters, one of the   one can get the underlying exception description.   Async:true (default)/false://When false, send sync request.     Example:  copy code code as follows: $ (function () {  $ (' #s1 '). Change (function () {    $.ajax ({  ' url ': ' Carinfo.do ',  ' type ': ' Post ',  ' data ': ' Carname= ' +$ (' #s1 '). Val (),  ' dataType ': ' xml ',  ' success ': function (data) { //data is returned by the server  //If an XML document is returned, we need to use the  //$ function to package $ (data) into a jquery //object for easy lookup.  //Append before emptying   $ (' #tb1 '). Empty ();  $ (' #tb1 '). Append (  ' <tr><td> manufacturer: '   + $ (data). Find (' company '). Text ()   + ' prices: ' + $ (data). Find (' price '). Text ()   + ' </td><td> body size: '   + $ ( data). FinD (' size '). Text ()   + ' Gate number: ' + $ (data). Find (' door '). Text ()   + ' </td><td> Displacement: '   + $ (data). Find (' Vol. '). Text ()   + ' accelerated performance: '   + $ (data). Find (' speed '). Text ()   + ' </td></tr> '); //To display the table   $ (' #tips '). Slidedown (' slow ');  settimeout (function () {  $ (' #tips '). fadeout (' slow '); },2000);  },  ' ERROR ': function () {  $ (' #tb1 '). Append (  "<tr><td colspan= ' 3 ' > This vehicle information is temporarily unavailable </ Td></tr> ");  $ (' #tips '). Slidedown (' slow '); } }); }); });    Example 2:   Solve Chinese garbled problem:  copy code code as follows: $.ajax ({  ' url ': ' Netctoss7/ajaxcode ',  ' type ': ' Post ',  ' data ': {name: value},  ' dataType ': ' json ',  ' async ':false,  ' success ': function (data) {  if (data) {  $ (' #msg _vercode '). Text (');  v1=true; }else{  $ (' #msg_verCode '). Text (' Authentication Code error '); } );       2. Two auxiliary methods     2.1.serialize ():   //Converts a form or form control contained in a jquery object into a query string.     2.2.serializeArray ():   //Convert to an array with an array element shaped like the {Name:fieldname,value:fieldval} object. The function of the  //serialization element is mainly used in AJAX requests, to assign values to data.     Description:  can only be used for forms or form controls   directly send the form name and corresponding value value, in the form of:name=value    example:  copy code as follows: $. Ajax ({})  //' data ': ' Carname= ' +$ (' #s1 '). Val (),  ' data ': $ (' #s1 '). Serialize (),   //' data ': {' Carname ': $ (' #s1 '). Val ()},  ' data ': $ (' #s1 '). Serializearray (),   
Related Article

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.