Usage of jquery ajax, ashx, and json
This article mainly summarizes the usage of jquery ajax, ashx, and json. If you need it, you can refer to it for help.
The simplified ajax call method provided by jquery is usually as follows:
The Code is 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 ","");
});
}
During development, when the return value in json format is to be accepted, the above method seems to be unable to work, and the above method seems to accept text lines of text. Therefore, jQuery's underlying Ajax implementation method is used.
There are many parameters for this method. For more information, see the help documentation. My general usage
The Code is 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) {// method executed when a request error occurs
Alert ("error! "+ ErrorThrown );
$ ("# DivWait"). hide ();
$ ("# BtnPost"). attr ("disabled ","");
},
Success: function (data, txtSataus) {// method executed when the request is successful
ShowContent (data. content, data. createdate );
$ ("# DivWait"). hide ();
$ ("# BtnPost"). attr ("disabled ","");
}
});
}
In the ashx code segment, set the returned format.
Context. Response. ContentType = "application/json ";
If it is the returned html or text, you can write it 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 data.
To convert an object to json format, the common method is to use the open source third-party class library json.net, Newtonsoft. Json. dll.
The JsonConvert. SerializeObject method can be converted. After the return value is in json format, jquery can use XXX. xxx to obtain the value.
When processing datetime format, JsonConvert returns an absolute value similar to 1198908717056. Therefore, when processing datetime, perform a conversion. The statement is as follows:
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter ();
// The Custom date format is used here. If this format is not used, the default format is ISO8601.
TimeConverter. DateTimeFormat = "yyyy '-'mm'-'dd''' HH': 'mm': 'ss ";
String output = JsonConvert. SerializeObject (m, Newtonsoft. Json. Formatting. Indented, timeConverter );
By the way, javascript has a natural processing capability for json-format data and is very compatible with json-format data.
For example:
The Code is as follows:
Function pppp (){
Var person = {"name": "jack", "age": 24, "sex": true };
Alert (person. name );
Alert (person. age );
Alert (person. sex );
}
Such code can be written directly, and code prompts can also be found in the vs2010 code editor. Very powerful.
The complete ashx code is 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
/// </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. If this format is not used, the default format is ISO8601.
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;
}
}
}
}