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進行解析,才能獲取引數。
相關文章
- vue3 獲取和設定路由引數Vue路由
- 微信小程式轉發onShareAppMessage設定path引數後在onload獲取不到值的原因和解決方法微信小程式APP
- js獲取url傳遞引數,js獲取url?號後面的引數JS
- Java設定JSON字串引數編碼JavaJSON字串
- $.ajax 中的contentType型別型別
- gofiber: 獲取引數Go
- js獲取帶#號連結後的引數JS
- 獲取url中?後面傳遞的引數
- ajax中POST請求與引數(請求體)設定
- python 獲取設定環境變數Python變數
- 獲取相應副檔名的ContentType型別型別
- JavaScript—獲取引數(23)JavaScript
- java後臺獲取cookieJavaCookie
- HttpServletRequest獲取header引數 signHTTPServletHeader
- Laravel request 獲取路由引數Laravel路由
- URL引數獲取/轉碼
- 獲取url上的引數
- 後臺接收Json請求引數相容陣列和單個物件JSON陣列物件
- swift 獲取通知設定Swift
- 關於POST傳值太大後端獲取不到後端
- 使用magicAPI對接python 檔案,上傳引數獲取不到回參問題APIPython
- jQuery : ajax獲取Status CodejQuery
- go語言獲取外部引數Go
- vue獲取位址列引數方法Vue
- http獲取get引數過濾HTTP
- js實現獲取URL引數JS
- C#獲取URL引數值C#
- axios 後端拿不到引數的處理iOS後端
- request 獲取不到 CookieCookie
- React router 4 獲取路由引數,跨頁面引數React路由
- ajax 引數詳解
- shell指令碼中main函式中$#獲取不到指令碼傳入引數個數淺析指令碼AI函式
- js_獲取與設定css變數的值JSCSS變數
- 怎麼在ajax外邊使用ajax裡面在後端獲取的資料後端
- SOLIDWORKS如何獲取模型中的引數Solid模型
- SpringBoot 攔截器獲取 @RequestBody 引數Spring Boot
- C# 解析獲取Url引數值C#
- JavaScript 獲取 url 傳遞引數值JavaScript
- 表單請求獲取路由引數路由