Http request and response in Angular 2.0

蓝咒 提交于 2019-12-04 07:37:28

The issue is not with chrome You must be using Proxying support in which dev-server makes use http-proxy-middleware package and the proxy options provided in this package is from underlying http-proxy library. one among them is

proxyTimeout:

timeout (in millis) when the proxy receives no response

The default value is ~120 seconds and a new request is made if the backend fails to respond within the stipulated time. overriding the default timeout with "timeout":360000 in the proxy config file will resolve the issue.

{
  "/api/*": {
     "target":  {
       "host": "localhost",
       "protocol": "http:",
       "port": 8080
     },
     "secure": false,
     "changeOrigin": true,
     "logLevel": "debug",
     "timeout":360000

  }
}

you also need to enable Cross-Origin Requests for a RESTful Web Service using @CrossOrigin annotation in your controller link.The @CrossOrigin is used to allow Cross-Origin Resource Sharing (CORS) so that our angular application running on a different server can consume these APIs from a browser.

    @CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
    @RestController
    @RequestMapping({"/api"})
    public class UserController {
           ......
}

I assume your angular is running on port 4200 with above configuration you can consume your API's.

NOTE: The proxy configuration is intended to proxy calls when running the dev server via ng serve. After you run ng build you are responsible for the web server and its configurations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!