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

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

问题描述

我是 angular 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天全站免登陆