gpt4 book ai didi

javascript - 从 angularjs 向 webAPI 发送 post 请求时参数为 null

转载 作者:行者123 更新时间:2023-11-27 23:53:56 25 4
gpt4 key购买 nike

我试图调用一个方法,但参数为空

服务器端代码

[HttpPost]
public IHttpActionResult PostRule(ActionRuleParameter actionRule)
{
// SOME CODE
return BadRequest();
}

public class ActionRuleParameter
{
public string action;
public string rule;
}

客户端代码

addRule: function ($scope) {
//var data = { "action": "post", "rule": { "ID": "1", "Name": "Ramesh", "PassFail": "Pass" } }

var data1 = { "action": "post","rule":"rule" };


$http({
url: urlContent + '/api/Rules',
method: "POST",
data: { "actionRule": data1 }
}).success(function (response) {
$scope.rules = response;
});;
},

actionrule 结果为 null。

最佳答案

您需要在参数前添加[FromBody]

    public IHttpActionResult PostRule([FromBody]ActionRuleParameter actionRule)
{
// SOME CODE
return BadRequest();
}

关于javascript - 从 angularjs 向 webAPI 发送 post 请求时参数为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com