简体   繁体   中英

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

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:

在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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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