hrishi
hrishi

Reputation: 1656

504 GATEWAY_TIMEOUT in ajax call

I have ajax call where I process large data and then reload the page in ajax success. Ajax call is

        $.ajax({
            type:'POST',
            cache:false,
            async:false,
            url: 'URL',
            data: "",
            timeout:0,
            beforeSend:function(msgg){   
                $('#loading').show();
            },
            success: function(data){
                if(data == "success")
                {
                    setTimeout(function(){
                        $('#loading').hide();
                        window.location.reload();
                    },5000);
                }
            }
        });

but it gets 504 GATEWAY_TIMEOUT and ajax call never comes in success. I need manual refresh.

Upvotes: 7

Views: 28017

Answers (4)

Irshad Khan
Irshad Khan

Reputation: 6046

Found working solution posted by #david-hoerster

As he said, If your error event handler takes the three arguments (XMLHttpRequest, textstatus, and message) when a timeout happens, the status arg will be 'timeout'

$.ajax({
    url: "/ajax_json_echo/",
    type: "GET",
    dataType: "json",
    timeout: 1000,
    success: function(response) { alert(response); },
    error: function(xmlhttprequest, textstatus, message) {
        if(textstatus==="timeout") {
            alert("got timeout");
        } else {
            alert(textstatus);
        }
    }
});​

With jsFiddle, you can test ajax calls -- it will wait 2 seconds before responding. I put the timeout set at 1 second, so it should error out and pass back a textstatus of 'timeout' to the error handler.

Upvotes: 0

Gabi Dj
Gabi Dj

Reputation: 617

The recommendation is to make more short calls to check status / retrieve data.

Here is the alternative if the above is not possible:

Notice: This solution is not recommended for production use or high traffic scenarios, make sure this action is performed on an isolated server.

If using Apache Web Server 2.4, the TimeOut directive is by default set to 60 seconds.

For more details see this article.

The web server will only keep the connection open for 60 seconds regardless the max_execution_time

While the max_execution_time in php.ini sets the execution time of php, the Apache TimeOut directive sets the maximum connection time.

Example:

  • if php max_execution_time is 120 sec, but TimeOut is 30 sec - you will get a 504 error
  • if php max_execution_time is 120 sec, but TimeOut is 300 sec - your script will execute for max 120 seconds, but your connection can stay alive for 300 seconds

If you didn't set any response code from php, PHP returns 200 if everything is OK, 500 if error occured.

When you get an unexpected HTTP Response code it's good to keep an eye on the web server too.

Upvotes: 2

hrishi
hrishi

Reputation: 1656

I think browser can not hold for long ajax call so I used ajax recall and processed data in parts. It solved problem.

Upvotes: 0

nikjohn
nikjohn

Reputation: 21852

504 GATEWAY_TIMEOUT errors normally occur when your API Gateway URL is not responsive. It could be some kind of internal Gateway error.

Here are some steps to troubleshoot:

  • Use a proxy like Postman to make the same exact request and see what the response is
  • Make sure your path is correct
  • If there are other requests that you make to the same gateway URL, maybe a GET request, try making those calls manually or using your code, to make sure the gateway is working fine
  • If you have access to the Gateway, restart it (this is possible if you have Microservices architecture, and Dockerisation etc)

Upvotes: 3

Related Questions