无法成功将JSON数据接收到.NET MVC Controller [英] Unable to successfully receive JSON data to .NET MVC Controller

查看:75
本文介绍了无法成功将JSON数据接收到.NET MVC Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是建立鼠标位置的JSON数组并将其传递给控制器​​.由于某种原因,我的json从控制器返回为未定义状态,谁能发现我的问题?

I'm aiming to build up a JSON array of mouse positions and pass them to a controller. For some reason my json is returning from the controller as undefined can anyone spot my problem?

// 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:

I then receive the jsontext from my .NET MVC Controller:

public JsonResult Save(string personResults)
{
    return Json(personResults);
}

如您所见,返回给AJAX的响应应该只是我发送给它的json-但是,尽管我的json似乎在构造Ok,并且我已经对其进行了测试,但我仍从服务器接收到未定义的值-有效.

As you can see the response back to the AJAX should just be the same json I sent to it - but I'm receiving undefined values back from the server even though my json seems to be constructing Ok and I've tested it - it's valid.

如果将保存"设置为字符串"类型,则收到此警报(new String(")));如果我将保存操作设置为接收"JsonResult"类型,则会收到此警报:

If I set Save to receive of type "string" I receive this alert "(new String(""))"; if i set the save action to receive of type "JsonResult" I receive this alert:

({ContentEncoding:null, ContentType:null, Data:null, JsonRequestBehavior:1})

我缺少完全显而易见的东西吗?我只想检查我的json是否已成功发送到控制器,以便稍后进行操作!

Am I missing something totally obvious? I just want to check that my json is being sent successfully to the controller so I can manipulate it later!

这是我的JSON格式:

Here's the format of my 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}
]}

谢谢!

推荐答案

我如何理解您想知道的问题如何将数据(输入参数)发送到MVC控制器的操作返回 JsonResult .

How I understand your question you want to know how to send data (input parameters) to the action of your MVC controller which will return JsonResult back.

您的第一个错误是操作 Save 的输入数据应不是JSON格式.MVC旨在与表单配合使用,因此,如果将任何数据发送到 Save 操作,则数据必须经过 url编码.例如,将"Bla Bla"作为操作 Save personResults 参数的输入,您的jQuery调用应遵循

Your first error is that input data of the action Save should be not in JSON format. MVC in designed more to work with forms, so if you send any data to the Save action the data must be url-encoded. For example to have "Bla Bla" as input of personResults parameter of the action Save your jQuery call should be following

$.ajax({
    url: "Home/Save",
    type: "POST",
    data: "personResults=Bla+Bla",
    success: function (data) {
        alert(data); },
    error: function (req, status, error) {
        alert("error! " + status + error );
    }
});

最好不要对字符串"Bla Bla"进行任何手动编码,并使用 data:{personResults:"Bla Bla"} 代替 data:"personResults = Bla + Bla".

More better not make any manual encoding of the string "Bla Bla" and use data: {personResults: "Bla Bla"} instead of data: "personResults=Bla+Bla".

如果要将更复杂的数据发送到MVC控制器,则可以执行此操作,但是必须将数据转换为JSON并手动返回.例如,如果您包含一个新动作 Save1 :

If you want to send more complex data to the MVC controller you can do this but you have to convert the data to JSON and back manually. For example if you include a new action Save1:

public JsonResult Save1 (string encodedAnswers) {
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    MyData data = serializer.Deserialize<MyData> (encodedAnswers);
    return Json (data);
}

其中 MyData 定义为

public class MyData
{
    public List<MyPosition> answers { get; set; }
}
public class MyPosition
{
    public int xpos { get; set; }
    public int ypos { get; set; }
}

(以相同的方式,您可以使用DataContractJsonSerializer 而不是 JavaScriptSerializer >.)相应的JavaScript代码可能如下

(In the same way you can use DataContractJsonSerializer instead of JavaScriptSerializer.) The corresponding JavaScript code could be following

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数据接收到.NET MVC Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆