<script> var app = angular.module('MyApp',[]); app.controller("QueryControllerController",function ($scope,$http) { $scope.submit = function () { if ($scope.QueryDescription) { var product = { "QueryDescription": $scope.QueryDescription,"CategoryID": $scope.CategoryID } $http.post('/api/querycontroller',JSON.stringify(product)). success(function (data,status,headers,config) { alert('Added Successfully' + headers); $('#formusers')[0].reset(); }). error(function (data,config) { alert(status + "," + data + "," + headers + "," + config); }); } }; } );
我的api控制器代码是:
[ResponseType(typeof(Query))] [HttpPost] public HttpResponseMessage Post([FromBody]Query Services) { Services.CommunityID = UserStatus.GetUserID(User.Identity.Name); Services = repository.Add(Services); var response = Request.CreateResponse<Query>(HttpStatusCode.Created,Services); string uri = Url.Route(null,new { id = Services.QueryID }); response.Headers.Location = new Uri(Request.RequestUri,uri); return response; }
"Message": "The request entity's media type 'text/plain' is not supported for this resource."
解决方法
[HttpPost] public HttpResponseMessage Post([FromBody]Productviewmodel product) {
也…
尝试https://stackoverflow.com/users/1267724/john建议的方法(在agunlar控制器中)作为这篇文章的答案:
change Content-type to “application/json” POST method,RESTful API
Posting a JSON object is quite easy in Angular. All you need to do is the following:
Create a Javascript Object
I’ll use your exact properties from your code.
var postObject = new Object(); postObject.userId = "testAgent2"; postObject.token = "testAgent2"; postObject.terminalInfo = "test2"; postObject.forceLogin = "false"; Post the object to the API
To post an object to an API you merely need a simple $http.post
function. See below:
$http.post("/path/to/api/",postObject).success(function(data){ //Callback function here. //"data" is the response from the server. });
Since JSON is the default method of posting to an API,there’s no need
to reset that. See this link on $http shortcuts for more @R_677_4045@ion.With regards to your code specifically,try changing your save method to include this simple post method.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。