ajax設定contentType=json後臺獲取不到引數

changuncle發表於2017-07-09

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進行解析,才能獲取引數。

相關文章