[英]How to handle parameters on C# server side sent from jQuery POST?
So I'm trying to send the following data from jQuery on the server side like so: 所以我想像这样从服务器端的jQuery发送以下数据:
var fd = new FormData();
fd.append('name', 'tyler');
fd.append('hello', 'world');
$.post('/NMISProduct/Index', { submitData: fd}, function(returnedData) {
console.log(returnedData);
}, 'json');
How do I handle this on the server side? 如何在服务器端处理此问题? Here is what I have, which I'm sure is very wrong: 这是我所拥有的,我确定这是非常错误的:
[HttpPost]
public string Index(string submitData)
{
return submitData;
}
I just want to return what I send to C# back to jQuery so I know it got there. 我只想将发送给C#的内容返回给jQuery,所以我知道它到达了那里。 What am I doing wrong? 我究竟做错了什么?
Your current approach ties you to FormData()
and it doesn't take advantage of JSON.Net which is happy and eager to deserialize your object so that you can consume it. 您当前的方法将您与FormData()
联系在一起,并且它没有利用JSON.Net,JSON.Net很高兴并渴望反序列化您的对象以便可以使用它。
If you truly want to test "full-loop", deserialize to a strongly typed object and return it back to the client as serialized json, building out the matching object on the client instead of using FormData()
. 如果您确实要测试“全循环”,请反序列化为强类型对象,然后将其作为序列化json返回给客户端,在客户端上构建匹配对象,而不是使用FormData()
。
$.post('/NMISProduct/Index', { name: 'tyler',hello: 'world' }, function(data) {
console.log(data);
});
[HttpPost]
public ActionResult Index(FormData submitData)
{
return Json(submitData);
}
public class FormData
{
public string Name { get; set; }
public string Hello { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.