c# – 不支持的媒体类型web api帖子
我正在使用
angularjs发布创建webapi的数据.它在我的本地系统上工作,但是当我上传到服务器上时它无法正常工作并返回错误,不支持的媒体类型.请帮我解决这个问题.
<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." 解决方法
删除[ResponseType(typeof(Query))]并使用ViewModel类作为参数
[HttpPost] public HttpResponseMessage Post([FromBody]ProductViewModel product) { 也… 尝试https://stackoverflow.com/users/1267724/john建议的方法(在agunlar控制器中)作为这篇文章的答案:
var postObject = new Object(); postObject.userId = "testAgent2"; postObject.token = "testAgent2"; postObject.terminalInfo = "test2"; postObject.forceLogin = "false"; Post the object to the API
$http.post("/path/to/api/",postObject).success(function(data){ //Callback function here. //"data" is the response from the server. });
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |