There are many types of contentType in ajax, the default is contentType=application/x-www-form-urlencoded;charset=utf-8;, if you set contentType=application/json;charset=utf-8; it will happen in the background In the case where the parameters cannot be obtained through context.Request.Form[], I will sort out the two methods of post and get below.
post by value
Front-end code, data is a json string:
function PostSendParams() {
$.ajax({
type: "post",
url: "Handler1.ashx",
contentType: "application/json;charset=utf-8;",
data: "{ \"contentType\": \"application/json\", \"param2\": \"18\" }",
dataType: "json",
success:function(data) {
alert("data=" + data);
},
error:function(error) {
alert("error=" + error);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Background value:
public void ProcessRequest(HttpContext context)
{
try
{
#region Form取值(不行)
//string contentType = context.Request.Form["contentType"].ToString();
//string param2 = context.Request.Form["param2"].ToString();
#endregion
#region InputStream取值(可以)
Stream stream = context.Request.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
string parameters = Encoding.Default.GetString(bytes);
JObject jObject = (JObject)JsonConvert.DeserializeObject(parameters);
string contentType = jObject["contentType"].ToString();
string param2 = jObject["param2"].ToString();
#endregion
}
catch (Exception ex)
{
context.Response.Write("error");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
Front-end code, data is a json object:
function PostSendParams() {
$.ajax({
type: "post",
url: "Handler1.ashx",
contentType: "application/json;charset=utf-8;",
data: { contentType: "application/json", param2: 18 },
dataType: "json",
success:function(data) {
alert("data=" + data);
},
error:function(error) {
alert("error=" + error);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Background value:
public void ProcessRequest(HttpContext context)
{
try
{
#region Form取值(不行)
//string contentType = context.Request.Form["contentType"].ToString();
//string param2 = context.Request.Form["param2"].ToString();
#endregion
#region InputStream取值(不行)
Stream stream = context.Request.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
string parameters = Encoding.Default.GetString(bytes);
JObject jObject = (JObject)JsonConvert.DeserializeObject(parameters);
string contentType = jObject["contentType"].ToString();
string param2 = jObject["param2"].ToString();
#endregion
}
catch (Exception ex)
{
context.Response.Write("error");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
get by value
Front-end code, data is a json string:
function GetSendParams() {
$.ajax({
type: "get",
url: "Handler1.ashx",
contentType: "application/json;charset=utf-8;",
data: "{ \"contentType\": \"application/json\", \"param2\": \"18\" }",
dataType: "json",
success: function (data) {
alert("data=" + data);
},
error: function (error) {
alert("error=" + error);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Background value:
public void ProcessRequest(HttpContext context)
{
try
{
#region QueryString取值(不行)
//string contentType = context.Request.QueryString["contentType"].ToString();
//string param2 = context.Request.QueryString["param2"].ToString();
#endregion
#region InputStream取值(不行)
Stream stream = context.Request.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
string parameters = Encoding.Default.GetString(bytes);
JObject jObject = (JObject)JsonConvert.DeserializeObject(parameters);
string contentType = jObject["contentType"].ToString();
string param2 = jObject["param2"].ToString();
#endregion
}
catch (Exception ex)
{
context.Response.Write("error");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
Front-end code, data is a json object:
function GetSendParams() {
$.ajax({
type: "get",
url: "Handler1.ashx",
contentType: "application/json;charset=utf-8;",
data: { contentType: "application/json", param2: 18 },
dataType: "json",
success: function (data) {
alert("data=" + data);
},
error: function (error) {
alert("error=" + error);
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Background value:
public void ProcessRequest(HttpContext context)
{
try
{
#region QueryString取值(可以)
string contentType = context.Request.QueryString["contentType"].ToString();
string param2 = context.Request.QueryString["param2"].ToString();
#endregion
#region InputStream取值(不行)
//Stream stream = context.Request.InputStream;
//byte[] bytes = new byte[stream.Length];
//stream.Read(bytes, 0, bytes.Length);
//string parameters = Encoding.Default.GetString(bytes);
//JObject jObject = (JObject)JsonConvert.DeserializeObject(parameters);
//string contentType = jObject["contentType"].ToString();
//string param2 = jObject["param2"].ToString();
#endregion
}
catch (Exception ex)
{
context.Response.Write("error");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
Summarize
When contentType=application/json;charset=utf-8;, the post value can only be obtained by parsing the data with InputStream in the background of a json string;
when contentType=application/json;charset=utf-8;, The get value can only be obtained by parsing the data with QueryString in the background of the json object.