JSON解析的阵列中的ASP.Net对象 [英] Parse Array of JSON Objects in ASP.Net
问题描述
我想学ASP.Net MVC,我想张贴JSON对象到服务器的阵列和发回给客户端。一切都很好,当我使用<一个href=\"https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en\"相对=nofollow>邮差,但它并没有实际网页上工作。我认为这个问题是不是与jQuery code的职位数组或ASP.Net code,是不是能够解析数组。
I am trying to learn ASP.Net MVC and I wanted to post array of JSON objects to the server and sent it back to the client side. Everything is fine when I use Postman, but it doesn't work on actual web page. I think the problem is either with jQuery code that posts the array or ASP.Net code that is not able to parse the array.
下面是我的控制器code:
Here is my controller code:
[System.Web.Mvc.HttpPost]
public ActionResult GetResult(List<Table> list)
{
return Json(list);
}
下面是我的对象声明:
public class Table
{
public int Id { get; set; }
public String Question { get; set; }
public int Answer { get; set; }
}
下面是jQuery的code,它的帖子中的数据:
Here is jQuery code that posts the data:
$.post("./GetResult", JSON.stringify(tableData), function (data, status) { alert(status); }, "json");
和资料表是JSON像这样的数组:
and tableData is an Array of JSON like this:
[
{
"Id": 500,
"Question": "where are you from",
"Answer": 2
},
{
"Id": 501,
"Question": "how old are you",
"Answer": 1
},
{
"Id": 502,
"Question": "what is your first car",
"Answer": 2
},
{
"Id": 503,
"Question": "do you have kids",
"Answer": 1
}
]
另外,我可以看到我的code穿过后控制器,但它是空的或为null。
Also, I can see that my code goes through the post controller but it is empty or null.
这里是我CSHTML文件的链接。
Here is the link of my csHTML file.
推荐答案
试着在你的请求,指定内容类型:
Try to specify content-type in your request:
$.ajax({
url: "./GetResult",
type: "POST",
data: JSON.stringify(tableData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) { alert(status); }
})
这篇关于JSON解析的阵列中的ASP.Net对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!