Introduction to the usage of jquery Ajax,ashx,json

Source: Internet
Author: User
Tags datetime json

  This article is mainly on the use of jquery Ajax,ashx,json a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you.

The simplified version of the Ajax invocation method provided by jquery is usually as follows:     code is as follows:     function post () {      $ ("#divWait"). Show (); nbsp     $ ("#btnPost"). attr ("Disabled", "disabled");      $.post (". /postit.ashx ",                      {        &N Bsp                 msgcontent: $ ("#msgContent"). Val ()         &NBS P            },                      F Unction (data) {                          if (Data.indexo F (' OK ') >-1 {                              Alert (data);                         }    &NB Sp                     else {                        &N Bsp    }                          $ ("#divWait"). hi De ();                          $ ("#btnPost"). attr ("Disabl Ed "," ");                     }); }     in development, accept JSON-formatted return value, the method above seems not to be able to do so, and the method above looks like a text line with text. Therefore, using jquery's underlying Ajax implementation method.   The method parameters are also many, specific to see the help document. My general usage     code is as follows:     function Dopostajax () {              $ ("#divWai T "). Show ();              $ (" #btnPost "). attr (" Disabled "," disabled ");    & nbsp         $.ajax ({                  URL: ' ... /postit.ashx ',                  Type: ' POST ',                dataType: ' JSON ',                  data: {msgcontent: $ ("#msgContent"). Val ()},                  timeout:60000,                  ERROR:FU Nction (XMLHttpRequest, Textstatus, Errorthrown) {/////////////////////                &NB Sp     Alert ("error!" + errorthrown);                      $ ("#divWait"). Hide ();                      $ ("#btnPost"). attr ("Disa Bled "," ");                 },            &NBS P     success:function (data, Txtsataus) {//Request to execute on successful method                 &NB Sp    Showcontent (Data.content, data.createdate);                      $ ( "#divWait"). Hide ();                      $ ("#btnPost"). attr ("Disab Led "," ");                              &NBSP ;    });         }     in ashx code snippet, to set the return format.   context. Response.ContentType = "Application/json";   If the return of HTML or text can be written as follows   context. Response.ContentType = "Text/plain";   If the return value set in the Ajax method is JSON, the format returned by the ASHX code must be JSON-formatted data. The common method of converting an object to Json format is to use the Open-source Third-party class library Json.net,newtonsoft.json.dll. The   Jsonconvert.serializeobject method can be converted. After the JSON format is returned, jquery can get the value in the Xxx.xxx way.   JsonConvert returns an absolute value similar to 1198908717056 when processing datetime format, so make a transition when dealing with DateTime. The specific statements are as follows:   Isodatetimeconverter timeconverter = new Isodatetimeconverter ();           //HereUse custom date format, if not used, default is ISO8601 format             Timeconverter.datetimeformat = "yyyy"-' MM '-' DD ' HH ': ' mm ': ' ss ';  string output = Jsonconvert.serializeobject (M, Newtonsoft.Json.Formatting.Indented, TimeConverter);   Here by the way, JavaScript has a natural ability to handle JSON-formatted data, which is very good for JSON-formatted data.   Example:     code as follows:     function pppp () {             var person = {"Name": "Jack", "Age": "Sex": true};             alert (person.name);  &nbs P          alert (person.age);             alert (person.sex); nbsp           &NBSP}   Such code can be written directly, in the VS2010 Code Editor can also have code hints. Very powerful.   ASHX complete code as follows: The   code is as follows: using system;  using system.collections.generic;  using system.linq;  Using system.web;  using system.threading;  using newtonsoft.json;  using Newtonsoft.Json.Converters;       namespace nnn {     ///<summary>     ///PostIt Summary description   & nbsp  ///</summary>      public class postit:ihttphandler      {    &NB Sp         public void ProcessRequest (HttpContext context)           {  &nbs P           context. Response.ContentType = "Application/json";              try              {                  String msgcontent = context. request["Msgcontent"]?? "";                  modelcontent m = new Modelcontent ()                   {                      AU Thor = "",                  &nbsp   CategoryID = -1,                      title = "",  &nbs P                   content = msgcontent,          &NB Sp           datetime = datetime.now,                      key = "",                      CreateDate = DateTime. now,                      lastmodifydate = datetime.now,  &nbs P                   IP = context. Request.userhostaddress                      };                      //bllcontent BLL = new Bllcontent ();  &N Bsp              //BLL. ADD (m); &nbsP                     Isodatetimeconverter timeconverter = new Isodatetime Converter ();                          /Use custom date format, if not used, default is ISO860 1 format                            Timeconverter.datetim Eformat = ' yyyy '-' mm '-' dd ' ' HH ': ' mm ': ' SS ';                  string output = J Sonconvert.serializeobject (M, Newtonsoft.Json.Formatting.Indented, TimeConverter);                  context. Response.Write (output);             }              catch (Exception ex)               {              &NB Sp   context. Response.Write (ex. Message);             } &NBsp                           public bool isreusable          {              get              {                  return false;          &N Bsp  }         }     } }  
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.