我正忙于我的第一个Angular web应用程序,我必须做一个帖子,我不确定如何将标题添加到请求中……就我在网上读到的,实现这一点的方法是使用angular-http的RequestOptions。我在网上看到了很多这样使用RequestOptions的例子:
我的代码:
let hdrs = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers: hdrs});
return this.http.post(this.API_URL + "StockTake/AddToStockTake", JSON.stringify(stockTakeModel), options)
.map((res: Response) => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'server error'));
}当我这样做时,我总是在我的chrome控制台上得到相同的错误,
Chrome错误:
Failed to load resource: net::ERR_CONNECTION_RESET在Firefox中,错误是:
[WDS] Disconnected!
main.bundle.js:51863:10
log
http://localhost:4200/main.bundle.js:51863:10
onSocketMsg.close
http://localhost:4200/main.bundle.js:51912:3
socket/sock.onclose
http://localhost:4200/main.bundle.js:85690:4
EventTarget.prototype.dispatchEvent
http://localhost:4200/main.bundle.js:51108:5
SockJS.prototype._close/<
http://localhost:4200/main.bundle.js:83235:5
Zone$1</ZoneDelegate</ZoneDelegate.prototype.invokeTask
http://localhost:4200/main.bundle.js:86029:21
Zone$1</Zone</Zone.prototype.runTask
http://localhost:4200/main.bundle.js:85918:28
ZoneTask/this.invoke
http://localhost:4200/main.bundle.js:86099:28
scheduleTask/data.args[0]有人向我建议http post方法的第三个选项采用RequestOptionsArgs类型,而不是RequestOptions类型……如果是这样,在这种情况下会是什么样子?我也尝试了{ headers: hdrs }而不是options作为第三个参数,但仍然得到相同的错误...
我不确定我是否找对了我的问题,当我在Chrome dev tools Networking选项卡中查看请求的请求标头时,它列出了以下内容:
Provisional headers are shown
Access-Control-Request-Headers:
content-type
Access-Control-Request-Method: POST
Origin: http://localhost:4200
Referer: http://localhost:4200/stockTake
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36我可以使用Insomnia Rest客户端发布到API,所以我知道这个问题与API无关……
我在网上看到的RequestOptions使用的方式和我用的一样:
https://scotch.io/tutorials/angular-2-http-requests-with-observables
https://angular.io/docs/ts/latest/guide/server-communication.html
发布于 2017-08-29 19:06:05
这被证明是后端的CORS问题……
https://stackoverflow.com/questions/41629306
复制相似问题