将JSON对象发布到C#ASP.NET WEB API
我无法在HTTP消息体中发布JSON对象 我甚至试过这个将JSON对象发布到C#ASP.NET WEB API,c#,.net,angularjs,json,http,C#,.net,Angularjs,Json,Http,我无法在HTTP消息体中发布JSON对象 我甚至试过这个 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 但它仍然不起作用 这是我的密码: <!DOCTYPE html> <html lang="en"> <script src="https://ajax.googleapis.com/ajax/libs
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
但它仍然不起作用
这是我的密码:
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="formCtrl">
<form novalidate>
type:<br>
<input type="number" ng-model="config.type"><br>
Id:<br>
<input type="text" ng-model="config.Id">
<br><br>
<button ng-click="submit()">Submit</button>
</form>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope,$http) {
$scope.config = {type:0, Id:"786"};
$scope.submit = function() {
var data = $scope.config;
//$http.post("http://localhost:8612/api/values/", data);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://localhost:8612/api/values/", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify(data));
alert("Done");
};
});
</script>
</body>
</html>
所以反序列化可能失败了。将请求模型设置为
对象
类型是错误的;相反,您应该定义一个合适的请求模型,并像这样使用它
public class ApiRequestModel
{
// define all your required properties client going to send
}
[HttpPost]
public IActionResult Post([FromBody] ApiRequestModel c)
所以反序列化可能失败了。将请求模型设置为
对象
类型是错误的;相反,您应该定义一个合适的请求模型,并像这样使用它
public class ApiRequestModel
{
// define all your required properties client going to send
}
[HttpPost]
public IActionResult Post([FromBody] ApiRequestModel c)