ASP.NET 控制器未从 Ajax POST 调用接收数据
ASP.NET controller does not receiving data from Ajax POST call
我基本上已经回答了所有问题,但似乎我遗漏了一些非常基本的问题。
我有一个简单的class:
public class Recebe
{
public string Name { get; set; }
public string Email { get; set; }
}
控制器POST动作:
[HttpPost]
public IActionResult ToggleLikeClicked(Recebe recebe)
{
return Json("Retorno");
}
还有一个 Ajax 电话:
$.ajax({
type: 'POST',
url: '/StartUps/ToggleLikeClicked',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Name': 'Sergio',
'Email': 'sergio@difiore.com.br'
}),
beforeSend: function () {
console.log('Before send');
},
success: function (response) {
console.log('success');
console.log(response);
},
complete: function () {
console.log('completed');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Status: ' + jqXHR.status + '; Error: ' + jqXHR.responseText);
},
});
从浏览器控制台,我知道整个过程都已执行,但在调试时 "receive" 为空。
怎么了?
尝试编辑您要发送至的 JSON:
{
'name': 'Sergio',
'email': 'sergio@difiore.com.br'
}
您也可以尝试在控制器中的参数中添加[FromBody]属性:
public IActionResult ToggleLikeClicked([FromBody] Recebe recebe)
我基本上已经回答了所有问题,但似乎我遗漏了一些非常基本的问题。
我有一个简单的class:
public class Recebe
{
public string Name { get; set; }
public string Email { get; set; }
}
控制器POST动作:
[HttpPost]
public IActionResult ToggleLikeClicked(Recebe recebe)
{
return Json("Retorno");
}
还有一个 Ajax 电话:
$.ajax({
type: 'POST',
url: '/StartUps/ToggleLikeClicked',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Name': 'Sergio',
'Email': 'sergio@difiore.com.br'
}),
beforeSend: function () {
console.log('Before send');
},
success: function (response) {
console.log('success');
console.log(response);
},
complete: function () {
console.log('completed');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Status: ' + jqXHR.status + '; Error: ' + jqXHR.responseText);
},
});
从浏览器控制台,我知道整个过程都已执行,但在调试时 "receive" 为空。
怎么了?
尝试编辑您要发送至的 JSON:
{
'name': 'Sergio',
'email': 'sergio@difiore.com.br'
}
您也可以尝试在控制器中的参数中添加[FromBody]属性:
public IActionResult ToggleLikeClicked([FromBody] Recebe recebe)