Today, when I was using Jquery's ajax method to send a request, I found that using the ashx file in the background could not receive the parameters passed in the ajax method. After checking the reason online, I found the problem. It turned out that I specified "contentType: 'application/json; charset=utf8'" in the $.ajax method, which caused the parameters passed to the server when processing the request in the ashx file.
The correct way to write it is as follows:
$.ajax({url: '/Handler.ashx?operFlag=test',type: 'POST',/*When requesting the ashx file, you must remove the contentType, and the data format is {"key","value"}; remember not to add double quotes outside the braces, as this will fail if the data cannot be retrieved on the ashx page*///contentType: 'application/json; charset=utf',data: {"key": "xdp","key":"Lonely wolf" },cache: false,dataType: 'text',success: function (data) {alert(data);},error: function (xhr) {alert("An error occurred, please try again later:" + xhr.responseText);}});In this way, you can use the following code in the ashx file to get the parameters passed by the $.ajax method, the code is as follows:
string key = context.Request["key"]; string key = context.Request["key"];
I used to use the $.post method to deal with ajax, so I didn't notice this problem. This time, since it was required by the project, I used $.ajax. I didn't expect to encounter the above problem. Fortunately, I found out the problem and solved the problem in time.
In addition, I have encountered a strange problem recently, "After submitting data to ashx using ajax, and after formatting the parameters with JSON.stringify, the code is as follows:
$.ajax({url: '/Handler.ashx?operFlag=test',type: 'POST',//JSON.stringify format parameter data: JSON.stringify({"key": "xdp-gacl","key": "White Tiger God Emperor"}),contentType: 'application/json; charset=utf',cache: false,dataType: 'json',success: function (data) {alert(data.key + "|" + data.key);},error: function (xhr) {alert("An error occurred, please try again later:" + xhr.responseText);}});As a result, the normal method of using context.Request["key3"] in ashx cannot obtain parameters, as shown in the figure below:
I was depressed for a long time and couldn't figure out why this happened. At first I thought it was caused by the code of contentType: 'application/json; charset=utf8', so I commented out the code:
$.ajax({url: '/Handler.ashx?operFlag=test',type: 'POST',//JSON.stringify format parameter data: JSON.stringify({"key": "xdp-gacl","key": "White Tiger God Emperor"}),//contentType: 'application/json; charset=utf',cache: false,dataType: 'json',success: function (data) {alert(data.key + "|" + data.key);},error: function (xhr) {alert("An error occurred, please try again later:" + xhr.responseText);}});But the result is still the same. Using context.Request["key3"] still cannot get the parameters. There is no way. Since the conventional method cannot be obtained, then look for another method. Baidu found a solution. You can get it by using the following method in ashx. First, write a general method to get the parameters, the code is as follows:
/// <summary>/// Get parameters /// </summary>/// <param name="context"></param>/// <returns></returns>private Dictionary<String, Object> GetParameter(HttpContext context){StreamReader reader = new StreamReader(context.Request.InputStream);//Get json string: strJson={"key":"xdp-gacl","key":"White Tiger God Emperor"}String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());JavaScriptSerializer jss = new JavaScriptSerializer();//Deserialize the json string into a Dictionary objectDictionary<String, Object> dicParameter = jss.Deserialize<Dictionary<String, Object>>(strJson);return dicParameter;}The GetParameter method returns a dicParameter object, and the dicParameter stores the parameters submitted from the $.ajax method to ashx, as shown in the figure below:
In this way, the passed parameters can be taken out from the dicParameter for processing. The complete code is as follows:
public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";string operag = 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);}}The above is the data sent by the ashx file obtained by the $.ajax() method introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!