AJAX post 数据到达 ASP.NET Core 2.1.1 控制器时为空
AJAX post data is null when it reaches the ASP.NET Core 2.1.1 controller
我有一个问题 post 从我的 razor 页面中的 java-脚本向控制器发送一个值。 - 收到的值始终为空。
我看到有人问过类似的问题,但一直没能找到正确的解决方法。
我在控制器中遇到了一个断点,(所以路由没问题)但是传递的参数值总是空的,而且不是 java-script 中最清晰的,我想要一些提示来自你们。
我基本上有下面的 java-脚本:
var clickButton = function (buttonId) {
$.ajax({
type: "POST",
async: false,
cache: false,
crossDomain: false,
contentType: "application/json; charset=utf-8",
url: "v1/buttons",
dataType: "json",
data: '{ buttonId:'+JSON.stringify( "buttonId" ) + '}',
success: function (result) {
//console.log("clickButton OK => " + result);
}, //success: function (result) {
timeout: 500 // 0.5 sec.
}); //$.ajax({
}//var clickButton = function(buttonNo) {
还有下面的 C# 控制器代码:
[Route( "v1/[controller]" )]
public class ButtonsController : Controller
{
...
[HttpPost]
public IActionResult OnButtonClicked( string buttonId )
{
// buttonId = null !!!
...
我在将布尔值传递给另一个控制器时遇到了类似的问题。
其中 bool 始终为 false.. 即默认值。
我想知道这是否是一个安全问题,当用户未经授权时不允许 post 包含数据...
问题出在JSON,我通过这个小改动得到了值,但它隐藏得很好。
var clickButton = function (buttonNo) {
console.log("clicked: " + buttonNo);
$.ajax({
type: "POST",
url: "v1/buttons/",
dataType: "json",
data: { "buttonId": buttonNo }, // < === this is where the problem was !!
success: function (result) {
}, //success: function (result) {
}); //$.ajax({
}//var clickButton = function(buttonNo) {
我已将控制器更改为接收用于标识按钮的字符串。
现在看起来像这样:
[Route( "v1/[controller]" )]
public class ButtonsController : Controller
{
...
[HttpPost]
public IActionResult PostButtonClick( string buttonId ) {
// buttonId now has a value !!!
}
}
我有一个问题 post 从我的 razor 页面中的 java-脚本向控制器发送一个值。 - 收到的值始终为空。
我看到有人问过类似的问题,但一直没能找到正确的解决方法。
我在控制器中遇到了一个断点,(所以路由没问题)但是传递的参数值总是空的,而且不是 java-script 中最清晰的,我想要一些提示来自你们。
我基本上有下面的 java-脚本:
var clickButton = function (buttonId) {
$.ajax({
type: "POST",
async: false,
cache: false,
crossDomain: false,
contentType: "application/json; charset=utf-8",
url: "v1/buttons",
dataType: "json",
data: '{ buttonId:'+JSON.stringify( "buttonId" ) + '}',
success: function (result) {
//console.log("clickButton OK => " + result);
}, //success: function (result) {
timeout: 500 // 0.5 sec.
}); //$.ajax({
}//var clickButton = function(buttonNo) {
还有下面的 C# 控制器代码:
[Route( "v1/[controller]" )]
public class ButtonsController : Controller
{
...
[HttpPost]
public IActionResult OnButtonClicked( string buttonId )
{
// buttonId = null !!!
...
我在将布尔值传递给另一个控制器时遇到了类似的问题。 其中 bool 始终为 false.. 即默认值。 我想知道这是否是一个安全问题,当用户未经授权时不允许 post 包含数据...
问题出在JSON,我通过这个小改动得到了值,但它隐藏得很好。
var clickButton = function (buttonNo) {
console.log("clicked: " + buttonNo);
$.ajax({
type: "POST",
url: "v1/buttons/",
dataType: "json",
data: { "buttonId": buttonNo }, // < === this is where the problem was !!
success: function (result) {
}, //success: function (result) {
}); //$.ajax({
}//var clickButton = function(buttonNo) {
我已将控制器更改为接收用于标识按钮的字符串。 现在看起来像这样:
[Route( "v1/[controller]" )]
public class ButtonsController : Controller
{
...
[HttpPost]
public IActionResult PostButtonClick( string buttonId ) {
// buttonId now has a value !!!
}
}