415不支持的媒体类型; Angular2转API [英] 415 Unsupported Media Type; Angular2 to API

查看:62
本文介绍了415不支持的媒体类型; Angular2转API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angle 2的新手,我遇到的问题是我找不到解决方案: 当我尝试从angular 2发布到API时,得到-415种不受支持的媒体类型.

i am new to angular 2 and i'm facing a problem which i cant find a solution: When i try to post from angular 2 to API i get - 415 unsupported media type.

Angular 2代码:

Angular 2 code:

 onSubmit(value: any) {
    // console.log(value.message);
    let headers = new Headers({ 'Content-Type': 'application/json'});
    let options = new RequestOptions({ headers: headers });
    let creds = 'statusuknown';
    let body = JSON.stringify(creds);
    this.http.post('http://localhost:1318/api/ActionItem', creds)
      .subscribe(
        () => {console.log('Success')},
        err => {console.error(err)}
      );
  }

我的控制器代码:

 // POST api/actionitem
        [HttpPost]
        public ActionItem Post( [FromBody]string str)// _id, string _status)
        {
            ActionItem item = new ActionItem( 313, str);
            return item;
        }

当我更改控制器代码以不从主体获取数据时,它可以工作,但是引用NULL.

when i change the controller code to not get data from body it works but refers NULL.

我的API调用屏幕截图: 请帮助&让我知道是否需要更多详细信息.

My API call screenshot: Please help & let me know if more details needed.

推荐答案

您定义了标头,但未使用它.将您的代码更改为

You defined headers, but didn't use it. Change your code to

this.http.post('http://localhost:1318/api/ActionItem', body, options).map(response => response.json())
      .subscribe(
        () => {console.log('Success')}
      );

这篇关于415不支持的媒体类型; Angular2转API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆