jquery ajax,ashx,json的用法總結(jié)
jquery提供的簡(jiǎn)化版的ajax調(diào)用方法通常如下:
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", "");
});
}
在開發(fā)的時(shí)候,要接受json格式的返回值時(shí),上面的方法貌似不能行,上面的方法貌似接受的是text的文本行。因此,采用jQuery的底層Ajax實(shí)現(xiàn)方法。
該方法參數(shù)也很多,具體可看幫助文檔。本人的常規(guī)用法
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) {//請(qǐng)求錯(cuò)誤 時(shí)執(zhí)行的方法
alert("error!" + errorThrown);
$("#divWait").hide();
$("#btnPost").attr("disabled", "");
},
success: function (data, txtSataus) {//請(qǐng)求成功時(shí)執(zhí)行的方法
showContent(data.content, data.createdate);
$("#divWait").hide();
$("#btnPost").attr("disabled", "");
}
});
}
在ashx代碼段,要設(shè)置好返回的格式。
context.Response.ContentType = "application/json";
如果是返回的html或者text的話可以如下寫法
context.Response.ContentType = "text/plain";
如果ajax方法中設(shè)置的返回值是json時(shí),ashx代碼返回的格式必須是json格式的數(shù)據(jù)。
把一個(gè)對(duì)象轉(zhuǎn)換成json格式,常用方法就是采用開源的第三方類庫(kù)json.net,Newtonsoft.Json.dll.
JsonConvert.SerializeObject方法就可以轉(zhuǎn)換了。返回json格式后,jquery就可以采用XXX.xxx的方式獲取值了。
JsonConvert在處理datetime格式的時(shí)候,會(huì)返回類似1198908717056的絕對(duì)值,因此,在處理datetime的時(shí)候,要做一下轉(zhuǎn)換。具體語(yǔ)句如下:
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
//這里使用自定義日期格式,如果不使用的話,默認(rèn)是ISO8601格式
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);
此處順便提一下,javascript對(duì)json格式的數(shù)據(jù)有著天生的處理能力,非常好的兼容json格式數(shù)據(jù)。
舉個(gè)例子:
function pppp() {
var person = { "name": "jack", "age": 24,"sex": true };
alert(person.name);
alert(person.age);
alert(person.sex);
}
這樣的代碼可以直接寫出來(lái),在vs2010的代碼編輯器中還可以有代碼提示。很強(qiáng)大。
ashx完整代碼如下:
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>
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();
//這里使用自定義日期格式,如果不使用的話,默認(rèn)是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;
}
}
}
}
相關(guān)文章
可輸入文字查找ajax下拉框控件 ComBox的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇可輸入文字查找ajax下拉框控件 ComBox的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-10-10
BootStrap表單驗(yàn)證中的非Submit類型按鈕點(diǎn)擊時(shí)觸發(fā)驗(yàn)證的坑
這篇文章主要介紹了BootStrap表單驗(yàn)證中的非Submit類型按鈕點(diǎn)擊時(shí)觸發(fā)驗(yàn)證的坑,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
jquery下實(shí)現(xiàn)overlay遮罩層代碼
下面是個(gè)應(yīng)用的小例子用來(lái)統(tǒng)一處理ajax請(qǐng)求中利用完全透明遮罩層組織用戶和界面元素交換,當(dāng)ajax出錯(cuò)時(shí)提示用戶2010-08-08
讓元素在網(wǎng)頁(yè)中可拖動(dòng)示例代碼
讓元素可拖動(dòng)的方法有很多,本文為大家介紹下使用jquery簡(jiǎn)單實(shí)現(xiàn)拖動(dòng)效果,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08

