数据未根据 abp.ajax 请求发送到服务器
Data not sending to server on abp.ajax request
我正在使用 ASP.NET 样板模板。我在让服务器接收我从 abp.ajax post 请求(代码如下所示)发送的数据时遇到问题。
控制台正在记录正确的数据(如屏幕截图 1 所示),但服务器未返回正确的数据。当我调试时,id 1000 的数据在服务器上显示为 0,即使控制台记录 1000 并且这是发送的对象。
截图一:
截图2:
Jquery:
$(document).on('click', '.default-customer-selection', function (e) {
e.preventDefault();
var info = {
toCustomerId: 1000 //Just trying to keep this simple for now
};
console.log(info);
abp.ajax({
url: abp.appPath + 'Portal/Catalog/SwitchCustomer',
data: JSON.stringify(info),
success: function (data) {
console.log('Data Returned: ' + data.personId);
}
});
});
Model/DTO 切换客户输入:
using System;
using System.Collections.Generic;
using System.Text;
namespace MySolution.Catalog.Dtos
{
public class SwitchCustomerInput
{
public long ToCustomerId { get; set; }
}
}
我的控制器功能:
public class CatalogController : MySolutionControllerBase
{
[HttpPost]
public JsonResult SwitchCustomer(SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}
}
我正在按照我在项目中看到的指南和其他预构建示例进行操作,这些示例已经在解决方案中正常运行:https://aspnetboilerplate.com/Pages/Documents/v1.5.2/Javascript-API/AJAX
我是不是漏掉了一步?非常感谢任何帮助!
abp.ajax
将选项作为对象获取。您可以传递在 jQuery 的 $.ajax 方法中有效的任何参数。这里有一些默认值:数据类型是'json',类型是'POST' 并且 contentType 是 'application/json'(因此,您调用 JSON.stringify 将 javascript 对象转换为 JSON 字符串在发送到服务器之前)。
您需要将 [FromBody]
属性添加到如下操作的参数中:
[HttpPost]
public JsonResult SwitchCustomer([FromBody]SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}
我正在使用 ASP.NET 样板模板。我在让服务器接收我从 abp.ajax post 请求(代码如下所示)发送的数据时遇到问题。
控制台正在记录正确的数据(如屏幕截图 1 所示),但服务器未返回正确的数据。当我调试时,id 1000 的数据在服务器上显示为 0,即使控制台记录 1000 并且这是发送的对象。
截图一:
截图2:
Jquery:
$(document).on('click', '.default-customer-selection', function (e) {
e.preventDefault();
var info = {
toCustomerId: 1000 //Just trying to keep this simple for now
};
console.log(info);
abp.ajax({
url: abp.appPath + 'Portal/Catalog/SwitchCustomer',
data: JSON.stringify(info),
success: function (data) {
console.log('Data Returned: ' + data.personId);
}
});
});
Model/DTO 切换客户输入:
using System;
using System.Collections.Generic;
using System.Text;
namespace MySolution.Catalog.Dtos
{
public class SwitchCustomerInput
{
public long ToCustomerId { get; set; }
}
}
我的控制器功能:
public class CatalogController : MySolutionControllerBase
{
[HttpPost]
public JsonResult SwitchCustomer(SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}
}
我正在按照我在项目中看到的指南和其他预构建示例进行操作,这些示例已经在解决方案中正常运行:https://aspnetboilerplate.com/Pages/Documents/v1.5.2/Javascript-API/AJAX
我是不是漏掉了一步?非常感谢任何帮助!
abp.ajax
将选项作为对象获取。您可以传递在 jQuery 的 $.ajax 方法中有效的任何参数。这里有一些默认值:数据类型是'json',类型是'POST' 并且 contentType 是 'application/json'(因此,您调用 JSON.stringify 将 javascript 对象转换为 JSON 字符串在发送到服务器之前)。
您需要将 [FromBody]
属性添加到如下操作的参数中:
[HttpPost]
public JsonResult SwitchCustomer([FromBody]SwitchCustomerInput input)
{
return Json(new { PersonId = input.ToCustomerId });
}