当调试Web api post操作的值始终为null时,我仍然尝试了所有操作。 我尝试更改标头,在发布的模型类上方添加了[JsonObject(MemberSerialization.OptOut)],并尝试了一个简单的Dto。什么都行不通... 这是传递用户数据的控制器功能:
class MyComponent extends React.Component<
{propItem1: string},
{stateItem1: string}
> {
constructor(props: ???) { // how to reference the prop type?
}
render() {
}
}
服务功能是:
$scope.Add = function () {
var user = {
ID: $scope.ID,
FirstName: $scope.FirstName,
LastName: $scope.LastName,
Age: $scope.Age,
Address: $scope.Address
};
$scope.usersRepo.push(user);
myService.addUser(user);
和Web api操作是:
var addUser = function (user) {
return $http.post(
usersApi + '/addNewUser',
JSON.stringify(user)),
{
headers: {
'Content-Type': 'application/json'
}
};
};
我的模特是这个:
[HttpPost, ActionName("addUser")]
public void Post([FromBody]UserModel value)
{
UsersDB.Add(value);
}
答案 0 :(得分:0)
使用这样的http发布方法。数据应该是不带json字符串化即可传递的对象。我建议您为http方法创建服务。
var obj = {
url: your url,
async: true,
method: 'POST',
headers: {
"content-type": "application/json; charset=utf-8",
}
};
if (typeof data != 'undefined' && typeof data != null) {
obj.data = data;
}
$http(obj).then(function() {}, function() {});
服务: // http方法
app.service('MethodProvider', ['$http', function ($http) {
var self = this;
self.get = function (url, data) {
var obj = {
url: url,
async: true,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
if (typeof data != 'undefined' && data != null) {
obj.params = data;
}
return $http(obj);
};
self.post = function (url, data) {
var obj = {
url: url,
async: true,
method: 'POST',
headers: {
"content-type": "application/json; charset=utf-8",
}
};
if (typeof data != 'undefined' && typeof data != null) {
obj.data = data;
}
return $http(obj);
};
self.put = function (url, data) {
var obj = {
url: url,
async: true,
method: 'PUT',
headers: {
'Content-Type': 'application/json'
}
};
if (typeof data != 'undefined' && data != null) {
obj.data = JSON.stringify(data);
}
return $http(obj);
};
self.delete = function (url) {
var obj = {
url: url,
async: true,
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
};
if (typeof data != 'undefined' && data != null) {
obj.data = JSON.stringify(data);
}
return $http(obj);
};
}]);