A summary of the usage of jquery Ajax,ashx,json _jquery

Source: Internet
Author: User
Tags datetime

The simplified version of the Ajax invocation method provided by jquery is usually as follows:

Copy Code code as follows:

Function post () {
$ ("#divWait"). Show ();
$ ("#btnPost"). attr ("Disabled", "disabled");
$.post (".. /postit.ashx ",
{
Msgcontent: $ ("#msgContent"). Val ()
},
function (data) {
if (Data.indexof (' OK ') >-1) {
alert (data);
}
else {

}
$ ("#divWait"). Hide ();
$ ("#btnPost"). attr ("Disabled", "");
});
}


When developing, to accept the return value in JSON format, the method above seems not to be able to do, the above method seems to accept text lines. Therefore, using jquery's underlying Ajax implementation method.

This method has many parameters, which can be seen in the Help document. My general usage

Copy Code code as follows:

function Dopostajax () {
$ ("#divWait"). Show ();
$ ("#btnPost"). attr ("Disabled", "disabled");
$.ajax ({
URL: '.. /postit.ashx ',
Type: ' POST ',
DataType: ' JSON ',
Data: {msgcontent: $ ("#msgContent"). Val ()},
timeout:60000,
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {/////////
Alert ("error!" + Errorthrown);
$ ("#divWait"). Hide ();
$ ("#btnPost"). attr ("Disabled", "");
},
Success:function (data, Txtsataus) {//the method to execute when the request succeeds
Showcontent (Data.content, data.createdate);
$ ("#divWait"). Hide ();
$ ("#btnPost"). attr ("Disabled", "");
}

});
}


In the ASHX code snippet, you want to set the return format.

Context. Response.ContentType = "Application/json";

If you are returning HTML or text, you can do the following

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 in 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 ();
The custom date format is used here, and if not used, the default is ISO8601 format
Timeconverter.datetimeformat = "yyyy"-' mm '-' dd ' ' HH ': ' mm ': ' SS ';
String output = Jsonconvert.serializeobject (M, Newtonsoft.Json.Formatting.Indented, TimeConverter);

Here, incidentally, JavaScript has a natural ability to handle JSON-formatted data, and is very good for JSON-formatted data.

As an example:

Copy Code code as follows:

function Pppp () {
var person = {' name ': ' Jack ', ' age ': ' Sex ': true};
alert (person.name);
alert (person.age);
alert (person.sex);
}

Such code can be written directly, and code hints are available in the VS2010 Code Editor. Very powerful.

ASHX complete code is as follows:

Copy Code code 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>
Summary description of PostIt
</summary>
public class Postit:ihttphandler
{

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Application/json";
Try
{
String msgcontent = context. request["Msgcontent"]?? "";
Modelcontent m = new Modelcontent ()
{
Author = "",
CategoryID =-1,
title = "",
Content = Msgcontent,
datetime = DateTime.Now,
Key = "",
CreateDate = DateTime.Now,
Lastmodifydate = DateTime.Now,
IP = context. Request.userhostaddress

};

Bllcontent BLL = new Bllcontent ();
Bll. ADD (m);

Isodatetimeconverter timeconverter = new Isodatetimeconverter ();
The custom date format is used here, and if not used, the default is ISO8601 format
Timeconverter.datetimeformat = "yyyy"-' mm '-' dd ' ' HH ': ' mm ': ' SS ';
String output = Jsonconvert.serializeobject (M, Newtonsoft.Json.Formatting.Indented, TimeConverter);
Context. Response.Write (output);
}
catch (Exception ex)
{
Context. Response.Write (ex. message);
}

}

public bool IsReusable
{
Get
{
return false;
}
}
}
}

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.