我正在尝试从angularjs控制器以$ http.post()的形式向asp.net Web api方法提交表单。但是它发送空值。这是我的代码
//angular controller
$scope.newpost = {};
$scope.save = function () {
console.log($scope.newpost); // it logs data accurately
$http.post('/api/NewPost', $scope.newpost).
success(function (data, status, headers, config) {
alert("ok");
});
}
//api
public HttpResponseMessage post(NewPost newpost) //fields are null here. I tried [FromBody] $ [FromURI], but no luck
{
posts.Add(newpost);
String id = newpost.Id; //saving is ok.
return Request.CreateResponse(HttpStatusCode.OK);
}
//model
public class NewPost
{
public String Title { get; set; }
public String Content { get; set; }
public String Tags { get; set; }
}
console.log($scope.newpost) displays-
Object {Title: "t", Content: "tt", Tags: "ttt"}
有什么帮助吗?
用这个:
console.log($scope.newpost.Title ); // it should logs data accurately
console.log($scope.newpost.Content );// it should logs data accurately
console.log($scope.newpost.Tags );// it should logs data accurately
$http.post('/api/NewPost', $scope.newpost).
success(function (data, status, headers, config) {
alert("ok");
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句