简体   繁体   English

如何处理从jQuery POST发送的C#服务器端的参数?

[英]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.

相关问题 从jquery数据表中的C#加载服务器端数据 - server side data loading from C# in jquery datatable 如何从C#服务器中的POST(或其他HTTP)请求访问参数? - How can I access parameters from a POST (or other HTTP) request in a C# server? 如何在使用jQuery post发送的post请求后获取表单参数 - how to get form parameters after post request sent with jQuery post 如何在 jquery 中读取 ac# 服务器端值 - How to read a c# server side value in jquery 如何在客户端检索由 c# 服务器 Response.Cookies.Append 方法发送的 cookie - How to retrieve cookie on the client side sent by c# server Response.Cookies.Append method 如何处理来自快递服务器的Ajax发布响应? - How to handle ajax post response sent form express server? 如何从服务器端访问通过 Jquery.getJSON 发送的数据? - How to access the data sent via Jquery.getJSON from the server side? jQuery表单插件:如何正确处理从服务器发送回的重定向请求 - jquery form plugin : how to correctly handle a redirect request sent back from the server 如何使用JQUERY使用AJAX将Javascript变量(从客户端)发布到PHP文件(服务器端) - How to POST a Javascript Variable (From Client Side) to PHP File (Server Side) with AJAX using JQUERY 如何使用 Jquery 客户端或 C# 服务器端验证泛卡号格式 - How to Validate Pan Card Number Format Using Jquery Client Side Or C# Server side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM