Home > Article > Web Front-end > Summary of usage of jquery ajax, ashx, json_jquery
The simplified version of the ajax calling method provided by jquery is usually as follows:
During development, when it is necessary to accept the return value in json format, the above method does not seem to work. The above method seems to accept text lines of text. Therefore, the underlying Ajax implementation method of jQuery is adopted.
This method also has many parameters, please see the help document for details. My regular usage
The JsonConvert.SerializeObject method can be used for conversion. After returning the json format, jquery can use XXX.xxx to obtain the value.
//Use a custom date format here, if not used, the default is ISO8601 format 'HH':'mm':'ss";
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);By the way, javascript has a natural ability to process json format data and is very compatible with json format data.
For example:
The code is as follows:
function pppp() {
alert(person.name); > alert(person.sex);
Such code can be written directly, and there can also be code prompts in the vs2010 code editor. Very powerful.
The complete code of ashx is as follows:
namespace nnn
{
///
/// PostIt 的摘要说明
///
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();
//这里使用自定义日期格式,如果不使用的话,默认是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;
}
}
}
}