ajax設定contentType=json後臺獲取不到引數
ajax中的contentType有多種型別,預設是contentType=application/x-www-form-urlencoded;charset=utf-8;,如果設定contentType=application/json;charset=utf-8;那就會發生在後臺無法通過context.Request.Form[]獲取引數的情況,下面我就post、get兩種方式進行梳理。
post傳值
前臺程式碼,data是json字串:
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);
}
});
}
後臺取值:
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");
}
}
前臺程式碼,data是json物件:
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);
}
});
}
後臺取值:
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");
}
}
get傳值
前臺程式碼,data是json字串:
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);
}
});
}
後臺取值:
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");
}
}
前臺程式碼,data是json物件:
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);
}
});
}
後臺取值:
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");
}
}
總結
當contentType=application/json;charset=utf-8;時,post傳值只有在data是json字串後臺用InputStream進行解析,才能獲取引數;
當contentType=application/json;charset=utf-8;時,get傳值只有在data是json物件後臺用QueryString進行解析,才能獲取引數。
相關文章
- 《OpenCV設定和獲取攝像頭引數》OpenCV
- js獲取url傳遞引數,js獲取url?號後面的引數JS
- JQuery中ajax和JSON的獲取jQueryJSON
- 微信小程式轉發onShareAppMessage設定path引數後在onload獲取不到值的原因和解決方法微信小程式APP
- 前臺怎樣獲取後臺ajax資料簡單介紹
- javascript獲取函式定義的引數個數JavaScript函式
- js獲取帶#號連結後的引數JS
- 獲取url中?後面傳遞的引數
- KindEditor:Ajax提交表單時獲取不到HTML內容HTML
- beego rest ful 請求引數為JSON怎麼獲取GoRESTJSON
- JavaScript—獲取引數(23)JavaScript
- 常用操作 / 獲取引數
- js獲取url引數JS
- jquery獲取url引數jQuery
- ajax中POST請求與引數(請求體)設定
- java後臺獲取cookieJavaCookie
- $.ajax 中的contentType型別型別
- Laravel request 獲取路由引數Laravel路由
- oracle獲取隱含引數Oracle
- jQuery獲取url引數值jQuery
- Js獲取URL地址引數JS
- 獲取位址列url連結?後面傳遞的引數
- DES加密後get獲取url引數無法解密問題加密解密
- 獲取安裝後Apache、MySQL、Nginx、PHP編譯時引數ApacheMySqlNginxPHP編譯
- 關於POST傳值太大後端獲取不到後端
- 回滾段損壞後的引數設定
- 後臺接收Json請求引數相容陣列和單個物件JSON陣列物件
- 獲取相應副檔名的ContentType型別型別
- vue獲取位址列引數方法Vue
- http獲取get引數過濾HTTP
- js實現獲取URL引數JS
- C#獲取URL引數值C#
- go語言獲取外部引數Go
- javascript獲取location.href引數JavaScript
- js獲取位址列的引數JS
- 獲取Oracle隱含引數資訊Oracle
- 獲取oracle的隱含引數Oracle
- jquery實現的ajax請求獲取json資料程式碼jQueryJSON