2

I am getting below error in the console of browsers.

Failed to load resource: 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. 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.

console shows below error:

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

The Ajax code:

$.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

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. Please help me to understand what's going wrong. Am I missing certain scenario to handle this ajax request

When I directly hit the server API, i get following response on browser:

FireFox:

response on Firefox

Chrome

response on Chrome

TheMadDeveloper
  • 1,587
  • 15
  • 28
Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • 1
    Sounds like you might be hitting some kind of timeout--both servers and browsers can terminate a request that it thinks is taking too long. Have you tried adding a `timeout` to your jquery ajax call? e.g. `$.ajax({ url : "resultValue.htm", timeout: 300000, ...etc...})`. Also, what kind of server is the api hosted on? It might not allow a request to go that long. – TheMadDeveloper Jun 18 '16 at 06:32
  • 1
    With a processing time of over 4 minutes, ajax doesn't really seam like the right approach here. I'd look at why processing takes that long and try to seriously real that in. If that just isnt doable, you might have a look at [service workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers) they might be better suited to this type of work – Wesley Smith Jun 18 '16 at 09:43
  • @TheMadDeveloper: I tried `timeout`, no luck. Its a Linux server. @DelightedDOD: It triggers some shell test scripts that runs on different servers in parallel and at last returns the file path when all reports are zipped – Shashank Vivek Jun 18 '16 at 10:09
  • Do you know what's handling the HTTP service on these servers? Is it Apache, nginx, Jetty, etc? Many of these are configured to timeout connections that are longer than some number of seconds (often less than a couple minutes). I would also second @DelightedD0D 's suggestion of using service workers or some other mechanism (such as polling). AJAX isn't really built for requests this long, so you'll likely run in to other issues. – TheMadDeveloper Jun 19 '16 at 04:54
  • @TheMadDeveloper DelightedD0D: From what you guys have suggested I am thinking to use websocket for this scenario. Do you think it's a good idea? although I am stuck http://stackoverflow.com/questions/37940540/websocket-code-not-working-on-tomcat-server-but-working-on-glassfish – Shashank Vivek Jun 21 '16 at 09:37
  • Did you try increasing the timeout in the apache httpd.conf file to see if it works? The variable is called 'Timeout' and its usually set to 2-3 minutes – FearlessHyena Jun 21 '16 at 15:43
  • may be this article will help you http://osperk.blogspot.in/2015/04/how-to-implement-ajax-with-java-servlet.html – uzaif Jun 23 '16 at 02:43
  • @TheMadDeveloper SHA1: Apologies for my poor knowledge. Its simply hosted on Tomcat server. Apache doesn't come into picture – Shashank Vivek Jun 23 '16 at 06:18

1 Answers1

4

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. Usually, the lowest timeout values are set by web servers.

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. There's likely a way to configure things like 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). AJAX isn't really built for requests this long, so you'll likely run in to other issues.

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. Do you think it's a good idea? although I am stuck stackoverflow.com/questions/37940540/

I think this is a wise decision, although websockets maybe be a little bit of an overkill. 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).

This post has some info that's specific to Tomcat. Also see this post for a detailed comparison of SSE and websockets.

Hope that helps!

Community
  • 1
  • 1
TheMadDeveloper
  • 1,587
  • 15
  • 28