Canceling ongoing Angular HTTP request does not stop

Is there a way to properly cancel an $https request in Angular when a new request is triggered? Despite using allMarkersRequest.abort(), the pending request still shows up in Chrome's dev tool. What am I missing?

In the code snippet below, allMarkersRequest.abort() should be triggered when allMarkersRequest is not null. However, even after calling deferredAbort.resolve(), it seems like the program still proceeds with .then().


        var allMarkersRequest = null;

        function getAllMarkersForSearch() {
            if (allMarkersRequest) {
                allMarkersRequest.abort();
                console.log('Aborting all markers request');
            }

            var deferredAbort = $q.defer();

            var url = Routing.generate('ajax_search_geo_all_markers');

            var req = $http({
                method: 'POST',
                url: url
            });

            allMarkersRequest = req.then(function (data) {
                createMarkers(data.data);

                $rootScope.$broadcast('map:markers:updated', markers);
            }, function (error) {
                // Display error message
            });

            allMarkersRequest.abort = function() {
                deferredAbort.resolve();
            };

            allMarkersRequest.finally(function () {
                allMarkersRequest.abort = angular.noop;
                deferredAbort = req = allMarkersRequest = null;
            });
        }
    

Answer №1

I forgot to add the timeout parameter in my request

Here is the code snippet:

var allMarkersRequest = null;

function getAllMarkersForSearch() {
    if (allMarkersRequest) {
        allMarkersRequest.abort();
        console.log('Aborting all markers request');
    }

    var deferredAbort = $q.defer();

    var url = Routing.generate('ajax_search_geo_all_markers');

    var req = $http({
        method: 'POST',
        url: url,
        timeout: deferredAbort
    });

    allMarkersRequest = req.then(function(data) {
        createMarkers(data.data);

        $rootScope.$broadcast('map:markers:updated', markers);
    }, function(error) {
        // Display error message
    });

    allMarkersRequest.abort = function() {
        deferredAbort.resolve();
    };

    allMarkersRequest.finally(function() {
        allMarkersRequest.abort = angular.noop;
        deferredAbort = req = allMarkersRequest = null;
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Firefox does not allow contenteditable elements to be edited if they are nested inside a parent element that is draggable

Here is a basic HTML example: <div draggable=true> <div contenteditable=true>can't edit me</div> </div> When testing in Chrome, I noticed that I was able to edit the contents of the contenteditable div, however, this functi ...

Smart method for organizing browsing history

I'm currently working on enhancing the navigation in an AJAX application. Here is my current approach: Whenever a user clicks on an AJAX link, the corresponding call is made and the hash is updated. Upon loading a new page, I verify if the hash exis ...

Patiently awaiting the completion of the entire page loading process

When using the methods below, we can determine if the entire page has finished loading. Sys.sleep(5) or remDr$executeScript("return document.readyState == 'complete';") or remDr$setTimeout(type = "page load", milliseconds = 10000) However, ...

Trigger the opening of a class or window upon pressing the button

Good evening, I'm attempting to create a functionality where pressing a specific button will open a window. However, I am unsure of how to achieve this using CSS classes. My initial thought was to add a new class in the CSS file and call it every ti ...

The NestJS error code 413 indicates that the payload size is too large for

I am currently utilizing nestjs and have encountered an issue where an endpoint only accepts files via multipart. When attempting to upload files that are less than 10MB, everything works perfectly fine. However, when trying to upload files larger than 10M ...

How can one effectively call a method in a separate controller?

Is there a recommended approach to accessing a function in controller A from controller B? One solution I found is to store the functions in a shared service. However, consolidating all functions and logic into this service doesn't seem like the most ...

Tracking page views through ajax requests on Google Analytics

I have implemented a method to log page views through ajax action when the inner page content is loaded. However, I am facing an issue where the bounce rate data is not getting updated and always shows 0%. The default Google Analytics page view is logged ...

Protractor: Locating Elements Based on Attributes

I have been looking for a specific element to test: <div class="alert alert-danger" role="alert" ng-show="notValid">Zugangsdaten eingeben</div> How do I locate this element to verify its visibility based on the ng-show attribute? The ng-show ...

Error message: Component is unable to access the $store property because it is undefined

After doing extensive research and reading numerous similar questions on various platforms, I am still unable to resolve my issue. I have a component containing a login form that triggers a method to dispatch a $store action for logging in the user via fi ...

Tips for accessing an Angular service from different Angular controllers

I am a beginner with angular js and I am currently exploring ways to call the service provided in the code snippet below from a controller. The service is defined as follows. app.factory('myappFactory', ['$http', function($http) { v ...

How can you retrieve the component's state from a higher level without relying on useContext?

I have a question regarding creating expanding flex cards. I found an example on Codepen that showcases what I'd like to achieve: https://codepen.io/z-/pen/OBPJKK In my code, I have a component called HomeButtons that generates these flex cards. With ...

Learn the process of utilizing signed URLs in conjunction with ReactJS to securely store files in AWS

I am currently working on a form where I need to attach images along with regular text inputs in order to submit data to my MongoDB. Here is the code for my post creation function: const [postData, setPostData] = useState({ text: '', i ...

Issues with running grunt serve

I'm encountering issues with running my project in WebStorm IDE. When I enter grunt serve, I am faced with the following errors: grunt serve Loading "connect.js" tasks...ERROR >> SyntaxError: C:\Users\TT\Documents\ES\fr ...

Identifying the color category based on the color code in the props and displaying JSX output

I am in need of a solution to display colors in a table cell. The color codes are stored in mongodb along with their descriptions. I am looking for a library that can determine whether the color code provided is in RGB or hex format, and then render it acc ...

What is the best way to rearrange an array in React?

I need help with manipulating an array of strings displayed in a list. Each string should have the ability to move up or down within the array. For example: const array = ["hello", "world", "cool"] moveUp("world", 1) // (moveUp: value:string, index: nu ...

Is it possible to implement formvalidation.io in a React project that is using Materialize-css?

Can the formvalidation.io plugin be used with React and Materialize-css in a project? My project consists of multiple input components that may or may not be within a form. I want to utilize formvalidation for input validation. However, I am unable to find ...

How can JavaScript nested AJAX requests and promises be properly chained together?

I'm currently facing a dilemma when it comes to setting the correct order of execution for the tasks at hand. Let's say I need to initiate an AJAX call, then handle the data and store it in IndexedDB using the idb-keyval library, which operates o ...

Using Express and Node JS to upload a text file into MongoDB

I have a simple text file that I want to upload and save in my MongoDB database. While I can successfully upload the file using basic HTML code, I am struggling with storing it. Here is what my file.html looks like: <!DOCTYPE html> <html> < ...

Repeatedly making jQuery ajax calls within a loop can lead to overwhelming the browser and

It's interesting to note that the response time from the database might be causing a browser to crash after 10 minutes. In the ajax call, all data from a specific table is being requested and then parsed into a div. The database table isn't very ...

How to access the CSS and JS files in the vendor folder using linemanjs

Currently, I am in the process of developing a web application utilizing linemanjs. However, I have encountered an issue where the vendor css and js files are not being referenced correctly when explicitly included. I would appreciate any guidance on what ...