在Angular中执行Http get方法而不重试,并等到服务器发送响应? [英] Perform Http get method in Angular with no retries and wait until server sends the response?
问题描述
我的控制器中有以下内容,在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()
}
在我的角应用程序中,我有:
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方法只有一次。
来自后端的响应快照 在端口8080上运行的Tomcat快照 正常情况下,当脚本未运行时服务器响应的快照 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 package
此处更多以及提供的代理选项在这个包中来自底层的 http-proxy库
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
proxyTimeout :
代理收到无响应时超时(以毫秒为单位)
timeout (in millis) when the proxy receives no response
默认值为~120秒(根据我的观察结果) )如果服务器未能在规定的时间(120s)内响应,则向服务器发出新的请求。
用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屋!