无法成功将JSON数据接收到.NET MVC控制器
我的目标是建立一个JSON鼠标位置数组,并将它们传递给控制器。出于某种原因,我的json从控制器返回,因为未定义。有人能发现我的问题吗无法成功将JSON数据接收到.NET MVC控制器,.net,asp.net-mvc,json,.net,Asp.net Mvc,Json,我的目标是建立一个JSON鼠标位置数组,并将它们传递给控制器。出于某种原因,我的json从控制器返回,因为未定义。有人能发现我的问题吗 // Attempt at declaring a JSON object var personResults = { "answers": [] }; // Onclick I fire in values from mouse event personResults.answers[count] = { "xpos": mouseX, "ypos": mo
// Attempt at declaring a JSON object
var personResults = { "answers": [] };
// Onclick I fire in values from mouse event
personResults.answers[count] = { "xpos": mouseX, "ypos": mouseY };
// AJAX Call
$.ajax({
url: "Save",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(personResults),
success: function (data) { alert(data.toSource()); },
error: function (req, status, error) {
alert("error! " + status + error + req);
}
});
然后,我从.NET MVC控制器接收jsontext:
public JsonResult Save(string personResults)
{
return Json(personResults);
}
return Json( new { personResults = personResults } );
正如您所看到的,对AJAX的响应应该与我发送给它的json相同——但是我从服务器接收到了未定义的值,尽管我的json似乎构造正常,并且我已经测试过了——它是有效的
如果我将Save设置为receive类型为“string”,我将收到此警报(新字符串(“”);如果我将保存操作设置为“JsonResult”类型的接收,我将收到此警报:
({ContentEncoding:null, ContentType:null, Data:null, JsonRequestBehavior:1})
我是不是遗漏了一些显而易见的东西?我只想检查我的json是否被成功地发送到控制器,以便以后可以操作它
以下是我的JSON格式:
{"answers":[
{"xpos":293,"ypos":415},{"xpos":293,"ypos":415},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},
{"xpos":293,"ypos":416},{"xpos":293,"ypos":416},{"xpos":293,"ypos":416}
]}
谢谢 我真的不明白控制器操作的用处,但是如果您只是想让它返回与作为输入接收到的相同的JSON字符串,您可以使用
Content
方法:
public ActionResult Save(string personResults)
{
return Content(personResults, "application/json");
}
您的$.Ajax调用正在将数据转换为字符串,而您返回的只是一个字符串。它可能无法解决如何处理这个问题,也可能没有必要 但是,建议您在控制器中尝试以下操作:
public JsonResult Save(string personResults)
{
return Json(personResults);
}
return Json( new { personResults = personResults } );
我如何理解您想知道的问题如何将数据(输入参数)发送到MVC控制器的操作,该控制器将返回
JsonResult
您的第一个错误是操作Save
的输入数据应该是而不是JSON格式的。MVC在设计上更多地用于表单,因此,如果您将任何数据发送到保存
操作,则数据必须url编码。例如,要将“Bla Bla”作为操作的personResults
参数Save
的输入,您的jQuery调用应该如下
$.ajax({
url: "Home/Save",
type: "POST",
data: "personResults=Bla+Bla",
success: function (data) {
alert(data); },
error: function (req, status, error) {
alert("error! " + status + error );
}
});
var personResults = {"answers":[{"xpos":293, "ypos":415 },{"xpos":293, "ypos":416}]};
// or var personResults = {answers: [{xpos: 293, ypos: 415},{xpos: 293, ypos: 416}]};
$.ajax({
url: "Home/Save1",
type: "POST",
data: {encodedAnswers: JSON.stringify(personResults)},
success: function (data) {
alert("answers[0].xpos="+data.answers[0].xpos); },
error: function (req, status, error) {
alert("error! " + status + error );
}
});
最好不要对字符串“Bla-Bla”进行任何手动编码,使用数据:{personResults:“Bla-Bla”}
而不是数据:“personResults=Bla+Bla”
如果您想向MVC控制器发送更复杂的数据,可以这样做,但必须将数据转换为JSON并手动返回。例如,如果您包括一个新操作Save1
:
public JsonResult Save1 (string encodedAnswers) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
MyData data = serializer.Deserialize<MyData> (encodedAnswers);
return Json (data);
}
(以相同的方式,您可以使用代替。)
相应的JavaScript代码可能如下所示
$.ajax({
url: "Home/Save",
type: "POST",
data: "personResults=Bla+Bla",
success: function (data) {
alert(data); },
error: function (req, status, error) {
alert("error! " + status + error );
}
});
var personResults = {"answers":[{"xpos":293, "ypos":415 },{"xpos":293, "ypos":416}]};
// or var personResults = {answers: [{xpos: 293, ypos: 415},{xpos: 293, ypos: 416}]};
$.ajax({
url: "Home/Save1",
type: "POST",
data: {encodedAnswers: JSON.stringify(personResults)},
success: function (data) {
alert("answers[0].xpos="+data.answers[0].xpos); },
error: function (req, status, error) {
alert("error! " + status + error );
}
});
谢谢你的深刻回应,这超出了我的期望!我基本上是想看看我的Json是否有效,因为我需要将它映射到一个数据类,如上面所示。现在我将字符串反序列化为一个类对象,并通过该对象循环对值执行操作!干杯=]感谢所有的回复,感谢您的宝贵时间!