ASHX file gets the data sent by the $.ajax () method _jquery

Source: Internet
Author: User
Tags httpcontext

Today, using the Ajax method of jquery to send a request, found in the background using the Ashx file can not receive the parameters passed in the Ajax method, the Internet to find out the cause of the problem is found, I was in the $.ajax method indicated the "ContentType:" Application/json; Charset=utf8 ' ", so it is not possible to get the parameters passed to the server side when processing the request in the Ashx file.

The correct wording is as follows:

$.ajax ({
URL: '/handler.ashx?operflag=test ',
type: ' POST ',
* *
when requesting ashx file to remove ContentType, there is The data
format is {"Key", "value"}; Remember not to add double quotes outside the curly braces, which
will fail on the ASHX page without the data
* *
//contenttype: ' Application/json ; Charset=utf ',
data: {"
key": "XDP",
"key": "Aloof Pale Wolf" 
},
Cache:false,
dataType: ' Text ',
success:function (data) {
alert (data)
},
error:function (XHR) {
alert ("An error occurred, please try again later:" + Xhr.responsetext);
}

The following code can be used in the ashx file to get the parameters passed by the $.ajax method, as follows:

String key = Context. request["Key"];

Used to be the $.post method to deal with Ajax, so did not pay attention to this problem, and this time the project needs, so the use of $.ajax, did not expect to encounter the above problems, fortunately found the problem and solve the problem in time.

In addition, recently encountered a strange problem, "with Ajax submission of data to ashx, after the use of json.stringify format parameters on the server side of the value?" ", 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 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);
}

Results The context is used in ashx. The conventional way to request["Key3" is to get no arguments, as shown in the following illustration:

Depressed for a long time, how also want to do not understand why so, at first thought is more contenttype: ' Application/json; Charset=utf8 ' This code, and then comment 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); c13/>},
error:function (XHR) {
alert ("An error occurred, please try again later:" + Xhr.responsetext);
}

But the result is the same, using the context. request["Key3"] or get no parameters, no way, since the conventional way to get, then find his method, Baidu, found a solution, in the ASHX use the following way can be obtained, first write a general method of obtaining 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 Emperor"}
String Strjson = Httputility.urldecode (reader. ReadToEnd ());
JavaScriptSerializer JSS = new JavaScriptSerializer ();
Deserializes the JSON string into a Dictionary object
dictionary<string, object> dicparameter = JSS. Deserialize<dictionary<string, object>> (Strjson);
return dicparameter;

The GetParameter method returns a Dicparameter object that dicparameter the arguments submitted to the ashx from the $.ajax method, as shown in the following illustration:

This allows you to remove the passed parameters from the Dicparameter, with the complete code as follows:

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);
}

The above is a small set to introduce the Ashx file to get $.ajax () method to send data, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.