C# 接收post到后台的json数据

web端post 到后台

$.ajax({
        type: "POST",
        url: "Test/GetJson",
        dataType: "JSON",
        data:JSON.stringify({id:id,name:name,info:info}),
        success: function (result) { 
        if(result.code='200'){
            console.log(success);}
        },
        error: function (err) {
        
        }
})

后台代码
先定义一个model

private class Student
{

    public string ID { get; set; }

    public string Name { get; set; }

    public string Info { get; set; }

}
反序列化方法

//反序列化
private object Deserialize(string responseHtml)
{
    System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
    T list = json.Deserialize(responseHtml);
    return list;
}

然后接收方法

public ActionResult GetJson(){
    System.IO.Stream postData = Request.InputStream;
    System.IO.StreamReader sRead = new System.IO.StreamReader(postData);
    string postContent = sRead.ReadToEnd();
    sRead.Close();
    var model = Deserialize(postContent) as Student;
    return Json(new { code = "200" }, JsonRequestBehavior.AllowGet);
}




你可能感兴趣的:(C#)