在 Angular 中执行 Http get 方法而无需重试并等待服务器发送响应? [英] Perform Http get method in Angular with no retries and wait until server sends the response?

查看:28
本文介绍了在 Angular 中执行 Http get 方法而无需重试并等待服务器发送响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器中有以下内容,在 Spring-Boot 应用程序中:

I have the following in my controller, in a Spring-Boot application:

@RequestMapping(method=RequestMethod.GET,value="/info")
public DataModel getinfodata()
{
    //this method runs some script which takes more than 3 minutes and sends the response back
    return run_xyz()
}

在我的 angular 应用程序中,我有这个:

In my angular app, I have this:

export class FetchService {
  private _url:string="/info";
    constructor(private _http:Http) { }
    getData():any
    {
      return this._http.get(this._url).map((res:Response)=>res.json()).
        catch(this.errorHandler).subscribe(data=>console.log(data));
    }
    errorHandler(error: Response){
      console.error(error);
      return Observable.throw(error || "Server Error");
    }

我目前面临的问题是 Http get 正在对服务器进行静默重试,结果,我的昂贵脚本被调用了 3 或 4 次.是否有一种方法可以使 get 只发出一个请求并重试 0 次,然后等待脚本完成,然后应该发回响应.我只调用 getData 方法一次.后端响应快照 Tomcat 8080 端口快照 正常情况下服务器响应的快照,当脚本未运行时 Angular CLI 189 秒后的最终响应

The issue I am facing currently is that the Http get is making silent retries to the server, and as result, my expensive script is getting called 3 or 4 times. Is there an approach by which the get will make only a single request with 0 retries and wait until the script is completed and then the response should be sent back.I am calling the getData method only once. Snapshot of response from backend Snapshot of Tomcat running on port 8080 Snapshot of server response in normal case, when the script is not running Angular CLI Final Response after 189 seconds

推荐答案

最终,我明白了为什么会这样 开发服务器使用 http-proxy-middleware 包 更多在这里 并且此包中提供的代理选项来自底层 http-代理库 http-proxy 选项.其中之一是

Eventually, I figured out why it is happening The dev-server makes use http-proxy-middleware package More Here and the proxy options provided in this package is from underlying http-proxy library http-proxy options. one among them is

代理超时:

代理没有收到响应时的超时时间(以毫秒为单位)

timeout (in millis) when the proxy receives no response

默认值为 ~120 秒(根据我的观察),如果服务器未能在规定的时间(120 秒)内响应,则会向服务器发出新请求.使用代理配置文件中的 "timeout":30000 覆盖默认超时解决了该问题.

The default value is ~120 seconds(based on my observations) if the server fails to respond within the stipulated time(120s) a new request is made to the server. overriding the default timeout with "timeout":30000 in proxy config file resolved the issue.

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

这篇关于在 Angular 中执行 Http get 方法而无需重试并等待服务器发送响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆