加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 不支持的媒体类型web api帖子

发布时间:2020-12-15 22:15:34 所属栏目:百科 来源:网络整理
导读:我正在使用 angularjs发布创建webapi的数据.它在我的本地系统上工作,但是当我上传到服务器上时它无法正常工作并返回错误,不支持的媒体类型.请帮我解决这个问题. scriptvar app = angular.module('MyApp',[]);app.controller("QueryControllerController",fun
我正在使用 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控制器中)作为这篇文章的答案:
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 information.

With regards to your code specifically,try changing your save method to include this simple post method.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读