控制器怎么接受ajax的data,通过Ajax将数据发送到控制器(Sending data to controller by Ajax)...

这篇博客讨论了如何通过Ajax以JSON格式发送数据到控制器。重点在于使用JSON.stringify将数据转换为字符串,并在Ajax请求中设置contentType为'application/json'。示例代码展示了如何将多个参数封装成一个列表并传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过Ajax将数据发送到控制器(Sending data to controller by Ajax)

通常我们通过ajax将数据发送到控制器,如下所示::

$.ajax({

type: "POST",

contentType: "application/json; charset=utf-8",

url: "/Controller/MyAction",

data: "{'Name':'" + id + "','Class':'" + cls + "'}",

dataType: "json",

请参阅我必须通过两个不同的参数发送到控制器的成员。 我想把它作为一个列表发送。

但我想要的是只用一个参数向控制器发送一个列表。 有可能,但我怎样才能做到这一点?

Usually we send data to controller by ajax like following::

$.ajax({

type: "POST",

contentType: "application/json; charset=utf-8",

url: "/Controller/MyAction",

data: "{'Name':'" + id + "','Class':'" + cls + "'}",

dataType: "json",

See I have to members to send to controller by two different parameters. I want to send it as a list.

but What I want is to send a list to the controller in just one parameter. Is it possible but how can I achieve that?

原文:https://stackoverflow.com/questions/22523791

更新时间:2020-01-31 15:54

最满意答案

JSON.stringify是这里的关键。

这是代码:

var myList = { List: [{ Name: "A", Class: "B" },

{ Name: "C", Class: "D" }] };

$.ajax({

type: 'POST',

url: '/{controller}/{action}',

cache: false,

data: JSON.stringify(myList),

dataType: 'json',

contentType: 'application/json; charset=utf-8'

});

JSON.stringify is the key here.

Here is the code:

var myList = { List: [{ Name: "A", Class: "B" },

{ Name: "C", Class: "D" }] };

$.ajax({

type: 'POST',

url: '/{controller}/{action}',

cache: false,

data: JSON.stringify(myList),

dataType: 'json',

contentType: 'application/json; charset=utf-8'

});

相关问答

尝试这个: var data = new FormData($(this)[0]);

而不是var data = new FormData(); Try this: var data = new FormData($(this)[0]);

instead of var data = new FormData();

像这样改变你的控制器。 public class WaitingListController : Controller

{

[HttpPost]

[Route("waitingList/apply")]

public string Apply(WaitingList wList)

{

return string.Format("Test");

}

}

Change your controller like this. public cla

...

尝试这个 : $.ajax({

type: "POST",

url: "page/save_data",

data: { "val1 ":val1,"val2": val2},

success: function(msg) {

alert(msg);

}

});

Try this : $.ajax({

t

...

$.ajax({

url : "/messages", //by rails convention doing a post simply to '/messages' should be handled by create action

method : "post",

data : { data_value: x },

dataType : "json", //this might be wrong?

success: function(response){

//d

...

因为您要发送json数据,请尝试使用@RequestBody注释而不是@RequestParam注释。 查看本文有关如何执行此操作的信息。 http://hmkcode.com/spring-mvc-json-json-to-java/ 对于简单的param传递,使用类似下面的内容并在服务器端使用@RequestParam,就像之前一样。 $.ajax({

type: "GET",

url: "http://localhost:80

...

在ajax调用中设置ContentType和DataType。 更新你的ajax代码,如下所示: $.ajax({

type: "POST",

dataType: "json",

contentType: "application/json",

url: "/Home/Test/",

data: JSON.stringify({ selectedNodesArray: selectedNodesArray }),

...

JSON.stringify是这里的关键。 这是代码: var myList = { List: [{ Name: "A", Class: "B" },

{ Name: "C", Class: "D" }] };

$.ajax({

type: 'POST',

url: '/{controller}/{action}',

cache: false,

data: JSON.str

...

你应该直接使用formData,不需要在对象中对其进行变形,如下所示: data:formData,

you should use formData directly, no need to warp it in an object, as follow: data:formData,

要通过POST请求发送JSON,您必须在doPost方法中读取请求的主体 。 这是一种方法: protected void doPost(HttpServletRequest hreq, HttpServletResponse hres)

throws ServletException, IOException {

StringWriter sw = new StringWriter();

IOUtils.copy(hreq.getInputStream(), sw, "UTF-8

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值