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.
我正在尝试学习ASP.Net MVC,我想将JSON对象数组发布到服务器并将其发送回客户端。当我使用Postman时,一切都很好,但它在实际的网页上不起作用。我认为问题是发布数组的jQuery代码或无法解析数组的ASP.Net代码。
Here is my controller code:
这是我的控制器代码:
[System.Web.Mvc.HttpPost]
public ActionResult GetResult(List<Table> list)
{
return Json(list);
}
Here is my object declaration:
这是我的对象声明:
public class Table
{
public int Id { get; set; }
public String Question { get; set; }
public int Answer { get; set; }
}
Here is jQuery code that posts the data:
这是发布数据的jQuery代码:
$.post("./GetResult", JSON.stringify(tableData), function (data, status) { alert(status); }, "json");
and tableData is an Array of JSON like this:
和tableData是一个JSON数组,如下所示:
[
{
"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
}
]
Also, I can see that my code goes through the post controller but it is empty or null.
此外,我可以看到我的代码通过post控制器,但它是空的或null。
Here is the link of my csHTML file.
这是我的csHTML文件的链接。
3 个解决方案
#1
2
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); }
})
#2
2
There is no need to JSON.stringify
in your $.post
call. Let jQuery handle that for you.
您的$ .post调用中不需要JSON.stringify。让jQuery为您处理。
#3
0
$.post("./GetResult", {list:tableData},
function (data, status)
{
alert(status);
}
, "json");
Json never passed object.
Json从未传递过对象。
using Newtonsoft.Json;
[HttpPost]
Public ActionResult GetResult(string list)
{
var obj = JsonConvert.DeserializeObject<List<Table>>(strStatus);
return Json(list)
}
or
要么
$.ajax({
url: "./GetResult",
type: "POST",
data: JSON.stringify(tableData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) { alert(status); }
})
#1
2
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); }
})
#2
2
There is no need to JSON.stringify
in your $.post
call. Let jQuery handle that for you.
您的$ .post调用中不需要JSON.stringify。让jQuery为您处理。
#3
0
$.post("./GetResult", {list:tableData},
function (data, status)
{
alert(status);
}
, "json");
Json never passed object.
Json从未传递过对象。
using Newtonsoft.Json;
[HttpPost]
Public ActionResult GetResult(string list)
{
var obj = JsonConvert.DeserializeObject<List<Table>>(strStatus);
return Json(list)
}
or
要么
$.ajax({
url: "./GetResult",
type: "POST",
data: JSON.stringify(tableData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) { alert(status); }
})