通过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
...