ashx檔案擷取$.ajax()方法發送的資料_jquery

來源:互聯網
上載者:User

今天在使用Jquery的ajax方法發送請求時,發現在後台中使用ashx檔案無法接收到ajax方法中傳遞的參數,上網查了一下原因後發現了問題所在,原來是我在$.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"]這種常規的方式是擷取不到參數的,如下圖所示:

鬱悶了好久,怎麼也想不明白為什麼會這樣,一開始以為是多了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"]還是擷取不到參數,沒辦法,既然常規的方式擷取不到,那就另尋他法吧,百度了一下,找到瞭解決辦法,在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()方法發送的資料,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.