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

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

发布时间:2020-12-17 06:59:27 所属栏目:安全 来源:网络整理
导读:我是角度2的新手,我遇到了一个无法找到解决方案的问题: 当我尝试从角度2发布到API我得到 – 415不支持的媒体类型. Angular 2代码: onSubmit(value: any) { // console.log(value.message); let headers = new Headers({ 'Content-Type': 'application/json
我是角度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)}
      );
  }

我的控制器代码:

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

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

我的API调用截图:

enter image description here


请帮助&如果需要更多细节,请告诉我.

解决方法

enter image description here

您定义了标题,但没有使用它.将您的代码更改为

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

(编辑:李大同)

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

    推荐文章
      热点阅读