【发布时间】:2018-02-15 14:17:37
【问题描述】:
我在一个 web api asp.net 核心应用上有以下方法
Api发布方式:
// POST api/values
[HttpPost]
public string Post([FromBody] List<ValuesToPost> values)
{
return values.Count.ToString();
}
它适用于邮递员:POST JSON(应用程序/json) 正文 -> 原始:
[{
Value: "asd",
Column:2,
Row:1,
IsPostBack: true
},
{
Value: "assddfs",
Column:2,
Row:1,
IsPostBack: true
}]
在我的 Angular 应用上:
组件一
sendDataToTheServerByPost(arrayOfObjects: any) {
this.dataService.postData(arrayOfObjects).subscribe(event => {
console.log("post");
});
}
数据服务.ts
public postData(body: any): Observable<any> {
let bodyToSend = JSON.stringify(body);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
return this.http.post(this.DataUrl, bodyToSend, { headers: headers }).map((response: Response) => {
return response.json().data;
}).catch(this.handleError);
}
我在 Chrome 控制台上收到以下错误:
Error
zone.js:2622 OPTIONS http://localhost:8080/api/values 415 (Unsupported Media Type)
我错过了什么,在 Postman 中,对 API post 方法的调用有效,但不是来自 Angular 应用程序。都在同一台开发计算机上运行。 CORS 问题我不这么认为,因为 Get Method 工作正常。还是我错了?建议?
【问题讨论】:
-
您的服务器 api 方法返回什么媒体类型?你的 api 接受 OPTIONS 请求吗?看起来 Angular 在发布之前已经感觉到了终点
-
你的服务器应该接受 OPTIONS 方法
标签: angular http post asp.net-core asp.net-web-api2