ajax:前后端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);
        }
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

后台取值:

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

前台代码,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);
        }
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

后台取值:

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传值

前台代码,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);
        }
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

后台取值:

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

前台代码,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);
        }
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

后台取值:

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

总结

当contentType=application/json;charset=utf-8;时,post传值只有在data是json字符串后台用InputStream进行解析,才能获取参数; 
当contentType=application/json;charset=utf-8;时,get传值只有在data是json对象后台用QueryString进行解析,才能获取参数。

猜你喜欢

转载自blog.csdn.net/qq_39985511/article/details/80019289
今日推荐