這篇文章主要介紹了ashx文件獲取$.ajax()方法發(fā)送的數據的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
今天在使用Jquery的ajax方法發(fā)送請求時,發(fā)現在后臺中使用ashx文件無法接收到ajax方法中傳遞的參數,上網查了一下原因后發(fā)現了問題所在,原來是我在$.ajax方法中指明了"contentType: 'application/json; charset=utf8'",所以才導致了在ashx文件中處理請求時無法獲取傳遞到服務器端的參數,
正確的寫法如下:
$.ajax({
url: '/Handler.ashx?operFlag=test',
type: 'POST',
/*
請求ashx文件的時候 要把contentType去掉,還有就是
data 格式為 {”key”,”value”};切記 不要再 大括號外面加雙引號,
這樣就會在ashx頁面取不到數據而失敗
*/
//contentType: 'application/json; charset=utf',
data: {
"key": "xdp",
"key":"孤傲蒼狼"
},
cache: false,
dataType: 'text',
success: function (data) {
alert(data);
},
error: function (xhr) {
alert("出現錯誤,請稍后再試:" + xhr.responseText);
}
});
這樣在ashx文件中就可以使用如下的代碼來獲取$.ajax方法傳遞的參數了,代碼如下:
string key = context.Request["key"];
string key = context.Request["key"];
以前一直都是用$.post方法來處理ajax,所以沒有注意到這個問題,而這次由于是項目需要,所以就使用了$.ajax,沒想到就遇到了上述的問題,好在找出了問題所在并且及時解決了問題。
另外,最近還遇到了一個奇怪的問題,"用ajax提交數據到ashx后,用JSON.stringify格式化參數后在服務器端取不到值?",代碼如下:
$.ajax({
url: '/Handler.ashx?operFlag=test',
type: 'POST',
//JSON.stringify格式化參數
data: JSON.stringify({
"key": "xdp-gacl",
"key": "白虎神皇"
}),
contentType: 'application/json; charset=utf',
cache: false,
dataType: 'json',
success: function (data) {
alert(data.key + "|" + data.key);
},
error: function (xhr) {
alert("出現錯誤,請稍后再試:" + xhr.responseText);
}
});
結果在ashx中使用context.Request["key3"]這種常規(guī)的方式是獲取不到參數的,如下圖所示:
郁悶了好久,怎么也想不明白為什么會這樣,一開始以為是多了contentType: 'application/json; charset=utf8'這句代碼造成的,于是把這句代碼注釋掉:
$.ajax({
url: '/Handler.ashx?operFlag=test',
type: 'POST',
//JSON.stringify格式化參數
data: JSON.stringify({
"key": "xdp-gacl",
"key": "白虎神皇"
}),
//contentType: 'application/json; charset=utf',
cache: false,
dataType: 'json',
success: function (data) {
alert(data.key + "|" + data.key);
},
error: function (xhr) {
alert("出現錯誤,請稍后再試:" + xhr.responseText);
}
});
可是結果還是一樣的,使用context.Request["key3"]還是獲取不到參數,沒辦法,既然常規(guī)的方式獲取不到,那就另尋他法吧,百度了一下,找到了解決辦法,在ashx中使用如下的方式就可以獲取到了,首先寫一個通用的獲取參數的方法,代碼如下:
/// <summary>
/// 獲取參數
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private Dictionary<String, Object> GetParameter(HttpContext context)
{
StreamReader reader = new StreamReader(context.Request.InputStream);
//得到json字符串:strJson={"key":"xdp-gacl","key":"白虎神皇"}
String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());
JavaScriptSerializer jss = new JavaScriptSerializer();
//將json字符串反序列化成一個Dictionary對象
Dictionary<String, Object> dicParameter = jss.Deserialize<Dictionary<String, Object>>(strJson);
return dicParameter;
}
GetParameter方法返回一個dicParameter對象,dicParameter就存放了從$.ajax方法中提交到ashx中的參數,如下圖所示:
這樣就可以從dicParameter中取出傳遞過來的參數作處理了,完整代碼如下:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string operFlag = context.Request["operFlag"];
if (operFlag == "test")
{
string key = context.Request["key"];
string key = context.Request["key"];
string resStr = key + "|" + key;
context.Response.Write(resStr);
}
else if (operFlag == "test")
{
Dictionary<String, Object> dicParameter = GetParameter(context);
string key = dicParameter["key"].ToString();
string key = dicParameter["key"].ToString();
string resStr = "{\"key\":\"" + key + "\", \"key\":\"" + key + "\"}";
context.Response.Write(resStr);
}
}
以上所述是小編給大家介紹的ashx文件獲取$.ajax()方法發(fā)送的數據,希望對大家有所幫助