简体   繁体   English

带有ajax的Java-ERR_EMPTY_RESPONSE-服务器处理请求时Ajax响应引发错误

[英]Java with ajax - ERR_EMPTY_RESPONSE - Ajax response throws error while server is processing the request

I am getting below error in the console of browsers. 我在浏览器控制台中遇到错误。

Failed to load resource: net::ERR_EMPTY_RESPONSE 无法加载资源:net :: ERR_EMPTY_RESPONSE

My ajax call works for all the button clicks, but this error is coming only for one button (lets say testExt button).On clicking on these buttons, a background script runs and execute some tests. 我的ajax调用适用于所有按钮单击,但是此错误仅适用于一个按钮(例如testExt按钮)。单击这些按钮时,将运行后台脚本并执行一些测试。 The only difference is that this testExt takes more time to complete its execution (nearly 4 min 27 secs) but the response comes to client at 4 min 16 secs in the error block of ajax even when the script is successfully executed at server's end. 唯一的区别是此testExt需要更多的时间来完成其执行(将近4分27秒),但是即使在服务器端成功执行了脚本,响应也会在4分16秒时在ajax error块中到达客户端。

console shows below error: 控制台显示以下错误:

Object {readyState: 0, responseText: "", status: 0, statusText: "error"}

The Ajax code: Ajax代码:

$.ajax({  
    type : "Get",   
    url : "resultValue.htm", 
    cache: false,
    data : "testName=" + name,  
    success : function(response) { 
        // success logic here
    },
    error: function(jqXHR, textStatus){
        console.log(jqXHR);
        alert('There has been server side error. Please contact TechEng team to get this fixed.')
    }
}); 

The response which is returned from such request is just one line string: 从这样的请求返回的响应只是一个字符串:

/xxxx/reports/2016-06-15/07-03-53-237-r1Qn/xxx-smoketest-report.html /xxxx/reports/2016-06-15/07-03-53-237-r1Qn/xxx-smoketest-report.html

I googled some questions on SO but I am not sure if its a java memory issue on server side as I tried to increase memory size as well. 我在SO上搜索了一些问题,但是我不确定是否在服务器端出现Java内存问题,因为我也尝试增加内存大小。 Please help me to understand what's going wrong. 请帮助我了解发生了什么问题。 Am I missing certain scenario to handle this ajax request 我是否缺少某些情况来处理此ajax请求

When I directly hit the server API, i get following response on browser: 当我直接点击服务器API时,我在浏览器上收到以下响应:

FireFox: 火狐:

在Firefox上回应

Chrome

在Chrome上回应

It does sound like you are running up against some kind of a timeout. 听起来好像您正在遇到某种超时。 There are any number of places where a 4+ minute request can be timed out--browser, web servers, application servers...maybe even network devices and operating systems. 在任何地方都可以超时4分钟以上的请求-浏览器,Web服务器,应用程序服务器...甚至网络设备和操作系统。 Usually, the lowest timeout values are set by web servers. 通常,最低的超时值由Web服务器设置。

Sounds like your running on tomcat, which I'm less familiar with, but you might be able to find some good information at https://tomcat.apache.org/connectors-doc/common_howto/timeouts.html . 听起来好像您在tomcat上运行,但我不太熟悉,但是您可以在https://tomcat.apache.org/connectors-doc/common_howto/timeouts.html上找到一些不错的信息。 There's likely a way to configure things like asyncTimeout . 可能有一种方法可以配置asyncTimeout类的asyncTimeout

However , I would reiterate my prior comment (which I know you've taken heed of): 但是 ,我要重申我的先前评论(我知道您已经注意了):

I would also second @DelightedD0D 's suggestion of using service workers or some other mechanism (such as polling). 我还要赞同@ DelightedD0D关于使用服务工作者或其他某种机制(例如轮询)的建议。 AJAX isn't really built for requests this long, so you'll likely run in to other issues. AJAX并不是真正为请求而构建的,因此您可能会遇到其他问题。

Even the documentation I pointed to recommends against setting these to extreme values. 即使是我指向的文档,也建议不要将其设置为极值。

As I mention, it sounds like you ARE going a different route, from your comment: 正如我提到的,这听起来像你要去不同的路线,从您的评论:

@TheMadDeveloper DelightedD0D: From what you guys have suggested I am thinking to use websocket for this scenario. @TheMadDeveloper DelightedD0D:从你们的建议来看,我正在考虑在这种情况下使用websocket。 Do you think it's a good idea? 您认为这是个好主意吗? although I am stuck stackoverflow.com/questions/37940540/ 虽然我被卡在stackoverflow.com/questions/37940540/

I think this is a wise decision, although websockets maybe be a little bit of an overkill. 我认为这是一个明智的决定,尽管websocket可能有点过分。 From what I gather, you don't really need the server and the client to be constantly talking while your task is being performed. 据我了解,在执行任务时,您实际上并不需要服务器和客户端保持不断的交谈。 Really, you just want to know when it's finished. 真的,您只想知道何时完成。

If you haven't gone too far down the websocket path, you might take a look at Server-Sent Events (SSE). 如果您在websocket路径上走得还很远,您可以看看服务器发送事件 (SSE)。

This post has some info that's specific to Tomcat. 这篇文章有一些特定于Tomcat的信息。 Also see this post for a detailed comparison of SSE and websockets. 另请参阅这篇文章 ,以详细比较SSE和Websocket。

Hope that helps! 希望有帮助!

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

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