我试图将一个简单的 JSON 发布到 C# Controller ,但我得到的是 null。我是 AngularJS 新手。
这是发布的 angularjs 代码
k = "{ 'id': 1,'name': 'A green door','price': 12.50}";
$http.post("/Inventory/PostSO", k)
.then(function (response) {
$scope.message = response.status;
},
function (error, status) {
$scope.message = error.error;
});
这是 C# 代码,但字符串数据始终为空
public ContentResult PostSO(string data)
{
return null;
}
我做错了什么?谢谢
最佳答案
首先设置请求 header 。
Content-Type - application/json
第二个 Controller 操作参数是字符串,它必须是一个模型,因为操作描述符尝试将发送的模型映射到 C# 对象,创建一个简单的类,并定义所有属性,例如 json 示例中的数据。
然后再试一次:)
关于javascript - 如何将 JSON 从 AngularJS 发布到 C# Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552448/