1

I have a JSP page which calls the Angularjs controller in js file. The Angularjs Controller calls the $http service to make a POST request to backend java based API (saveRecordToDB). Below is the code snippet. After receiving the response, the frontend displays the Successful message on the frontend.

When I execute this code for data which takes less time to complete the API (saveRecordToDB), around 1-2 minutes, then the Successful message is displayed on page.

But when I execute this code for data which take long time to complete the API (saveRecordToDB), around 40-50 mins, then the page status is not changed to Successful. I can see that all the steps in API (saveRecordToDB) are completed for both the request. It seems that for long duration request, $http doesn't catch the response from API.

JSP Page:

<div class="row" ng-if="!downloadFile">
    <div class="col-sm-1" ng-if="brokerUploadCount > 0"><button ng-click="approve()" class="pull-right btn btn-success">Approval</button></div>
    <div class="col-sm-1"><button ng-click="reject()" class="pull-left btn btn-danger">Reject</button></div>
</div>
<br>
<div class="row" ng-if="downloadFile">
    <div class="col-sm-4">
        <div class="col-sm-2"><b>SUCCESSFUL</b></div>
    </div>
<script type="text/javascript" src="js/controller/Upload.js"></script>

Upload.js file:

pos.controller('excelCtrl', ['$scope', '$http', 'appService', 'FileUploader', function($scope, $http, appService, FileUploader) {

    $scope.approve = function(){
        var uploadUrl = "api/saveRecordToDB" ;
        
        var dataSet = {
                "period":$("#period").val(),
        };
        $http({
            method: 'POST',
            url: uploadUrl,
            cache: false,
            //timeout: 60 * 60 * 1000,
            params: dataSet
        }).success(function(data){
            appService.showStatus("success");
            $scope.downloadFile = true;
            $scope.fileName = "";
        });
    }
}]);

I tried to change the timeout for $http request. But not sure if its correct. Also tried for async call.

Please can someone suggest how I can make the $http service to wait for long duration till the backend API execution completes.

Jack1909
  • 11
  • 3
  • 1
    50 minutes seems like a long time to wait for a request to complete. Have you thought about changing the UI so the user gets a quicker response and you can show a message like "Your database update is pending" to them? – Richard Hunter Oct 06 '20 at 17:27
  • Actually, once the request is completed, the download button should be enabled on UI. We cant enable the button before request is completed. Any way to accomplish it ? Also cant reduce the backend time. – Jack1909 Oct 07 '20 at 02:30
  • 50 minutes to wait for an Ajax request seems beyond unusual to me. That's why I'm wondering if you might consider a different approach. However, if you do need to do this there's a question about waiting a long time for Ajax calls here https://stackoverflow.com/questions/7297833/how-long-will-the-browser-wait-after-an-ajax-request that may be useful to you. – Richard Hunter Oct 07 '20 at 02:37
  • 1
    50 minutes is a long time to wait for a request to complete. You may try to post your data in chunks so in your UI you can set your form in steps, this type of UI helps you posting data step by step. Actually most browsers have its default timeouts, when that will be hit ajax call is returned due to timeout it will return with an error status of "timeout". – Viral Patel Oct 07 '20 at 14:04
  • If you are using HTTP/1.1 then, have you tried increasing the "keep-alive" header value on the server response? Refer: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive – Abhilash Nayak Oct 10 '20 at 14:32

0 Answers0