415不支持的媒体类型; Angular2到API

Ore*_*ger 8 api http-post angular

我是角度2的新手,我面临一个问题,我无法找到解决方案:当我尝试从角度2发布到API我得到 - 415不支持的媒体类型.

Angular 2代码:

 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)}
      );
  }
Run Code Online (Sandbox Code Playgroud)

我的控制器代码:

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

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

我的API调用截图: 在此输入图像描述 如果需要更多详细信息,请帮助并告诉我们.

Vic*_*hin 1

在此输入图像描述您定义了标头,但没有使用它。将您的代码更改为

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