简体   繁体   English

Angular2:Angular-http RequestOptions混淆

[英]Angular2: Angular-http RequestOptions confusion

I'm busy with my first Angular web app, I have to do a post and I'm very unsure how to add the headers to the request... As far as I've read online the way to do this is by using angular-http's RequestOptions. 我正忙于我的第一个Angular Web应用程序,我必须写一篇文章,而且我不确定如何将标头添加到请求中...据我在线阅读的方法是通过使用angular-http的RequestOptions。 I see many examples online where RequestOptions is used in this way: 我在网上看到许多示例,其中以这种方式使用RequestOptions:

My code: 我的代码:

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'));
  }

When I do this I always get the same error in my chrome console, 当我这样做时,我在chrome控制台中总是遇到相同的错误,

Chrome error: Chrome错误:

  Failed to load resource: net::ERR_CONNECTION_RESET

In Firefox the error is: 在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]

It's been suggested to me that the third option of the http post method takes a RequestOptionsArgs type and not a RequestOptions type... If so how would that look in this instance? 有人向我建议,http post方法的第三个选项采用RequestOptionsArgs类型,而不是RequestOptions类型。如果是这样,在这种情况下情况如何? I've also tried { headers: hdrs } instead of options as the third argument but still getting the same error... 我也尝试使用{ headers: hdrs }代替选项作为第三个参数,但是仍然遇到相同的错误...

I'm not sure if I'm looking in the right place for my issue, when I look under the request headers of the request in Chrome dev tools Networking tab it lists the following: 当我在Chrome开发者工具的“网络”标签中查看请求的请求标题下方时,不确定是否在正确的位置查找问题,它列出了以下内容:

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

I can post to the API using the Insomnia Rest client so I know the issue isn't API related... 我可以使用Insomnia Rest客户端发布到API,所以我知道问题与API不相关...

the Online places I've seen RequestOptions used the way I do use it: 我见过的在线场所RequestOptions的使用方式如下:

https://scotch.io/tutorials/angular-2-http-requests-with-observables https://scotch.io/tutorials/angular-2-http-requests-with-observables

https://angular.io/docs/ts/latest/guide/server-communication.html https://angular.io/docs/ts/latest/guide/server-communication.html

事实证明,这是后端的CORS问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM