首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2: Angular-http RequestOptions混淆

Angular2: Angular-http RequestOptions混淆
EN

Stack Overflow用户
提问于 2017-01-13 15:19:17
回答 1查看 1.6K关注 0票数 0

我正忙于我的第一个Angular web应用程序,我必须做一个帖子,我不确定如何将标题添加到请求中……就我在网上读到的,实现这一点的方法是使用angular-http的RequestOptions。我在网上看到了很多这样使用RequestOptions的例子:

我的代码:

代码语言:javascript
复制
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错误:

代码语言:javascript
复制
  Failed to load resource: net::ERR_CONNECTION_RESET

在Firefox中,错误是:

代码语言:javascript
复制
[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选项卡中查看请求的请求标头时,它列出了以下内容:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2017-08-29 19:06:05

这被证明是后端的CORS问题……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41629306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档