我是 Angular 2 的新手,我面临着一个无法找到解决方案的问题:
当我尝试从 Angular 2 发布到 API 时,我得到 - 415 不支持的媒体类型。
角度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。
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')}
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)