为什么我没有在ASP.Net MVC控制器中获取AJAX数据?

时间:2021-07-16 04:14:23

I have this code on my page:

我的页面上有这个代码:

polyData = [1.2, 1.3, 1.4];
document.getElementById('sentDiv').innerHTML = JSON.stringify(polyData);

$.post('/Home/updateData', $.param({
  data: JSON.stringify(polyData)
}, true), function(data) {
  $("#dataDiv").html("this is data: " + JSON.stringify(data));
});

This is in my controller:

这是在我的控制器中:

[HttpPost]
public JsonResult updateData(List<double> polyData)
{
  return Json(polyData);
}

When I made the AJAX call my updateData in the controller is called, but polydata is null and no data is passed to it via the AJAX call. I can see on the web page (sentData div) that polyData has valid values. Why is data not passed correctly?

当我进行AJAX调用时,调用控制器中的updateData,但是polydata为null,并且没有数据通过AJAX调用传递给它。我可以在网页(sentData div)上看到polyData具有有效值。为什么数据没有正确传递?

How can I see what is the JSON message that received by controller and how controller tried to extract them to polyData and debug the process?

如何查看控制器收到的JSON消息以及控制器如何将它们提取到polyData并调试进程?

1 个解决方案

#1


0  

The issue is because the action in your controller is expecting a parameter named polyData, yet your AJAX request is sending data. You simply need to change the name of the value you send:

问题是因为控制器中的操作需要一个名为polyData的参数,但是您的AJAX请求正在发送数据。您只需更改发送值的名称:

$.param({
  polyData: JSON.stringify(polyData)
}, true)

Also note that you may also note need the $.param or the JSON.stringify calls, but this depends on how your model and model binder is structured.

另请注意,您可能还需要注意$ .param或JSON.stringify调用,但这取决于模型和模型绑定器的结构。

#1


0  

The issue is because the action in your controller is expecting a parameter named polyData, yet your AJAX request is sending data. You simply need to change the name of the value you send:

问题是因为控制器中的操作需要一个名为polyData的参数,但是您的AJAX请求正在发送数据。您只需更改发送值的名称:

$.param({
  polyData: JSON.stringify(polyData)
}, true)

Also note that you may also note need the $.param or the JSON.stringify calls, but this depends on how your model and model binder is structured.

另请注意,您可能还需要注意$ .param或JSON.stringify调用,但这取决于模型和模型绑定器的结构。