Strategies for Mitigating Duplicate Requests in AngularJs with just one click

I am facing an issue with my AngularJS application where clicking on a link triggers the API call twice. I'm not sure why this happens and how to fix it.

My service: SomethingService

 function getData() {
    return apiSettings.getApiInformation().then(function (response) {
        var url = response.data.Url + "/odata/Something?$expand=Something";

            var data = {
                url: url,
                type: "GET",
                token: response.data.Token,
                data: {},
                async: true,
                cache: false,
                headers: {
                         "accept": "application/json; charset=utf-8",
                         'Authorization': 'Bearer ' + response.data.Token
                       },
                dataType: "json",
                success: {},
                error: {},
                complete: {},
                fail:{}
            };
            return $http(data);
    });   
}

Api Settings :

angular.module('myApp.services')
.factory('apiSettings', apiSettings);

apiSettings.$inject = ['$http'];

function apiSettings($http) {
    return {
            getApiInformation: function () {
               return $http.get(baseUrl+ '/api/GetApiInformation', {cache: true});
            }
    }

}

SomethingController:

var vm = this;
function getSlots(filterCriteria, selectedValue) {

    somethingService.getData().then(function (response) {
        if (response && response.value.length > 0) {               
            vm.someData = response.value;
        }
    });

View: Clicking on a link triggers getSlots, which results in duplicate requests being sent.

 <a ui-sref="something" class="action-icons" id="slotNav"><i class="fa fa-square-o fa-fw"></i>
something
</a>

This view displays data

<div ng-repeat="data in vm.someData">
        <p> {{data.Name}}</p>
 </div>

Issue: When triggering once, the browser sends duplicate requests as seen below. The first call does not have a callback, while the second one does:

someuls?$expand=something&_=1432722651197   
someuls?$expand=something&_=1432722651197

Answer №1

After encountering a similar issue, I was able to solve it by following the instructions in this helpful answer. In my case, I had mistakenly declared "ng-controller" both in the HTML and using routeProvider, which caused conflicts. The problem was resolved by removing the "ng-controller" property from the HTML code.

I hope this information proves useful to others facing the same dilemma.

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

Exploring File Read and Write Functionality in Angular 4

I want to develop an offline Task List in Angular 4. However, I am facing difficulty in finding a method to save data in a file on the client side using JavaScript or Angular. My current approach involves using browser's localStorage, but it is slow ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

Creating a character jump animation on an HTML5 canvas

While following a tutorial on creating character animations, I encountered an issue. The tutorial, located at , made it easy to make the character move left (the right movement was already implemented). However, I am struggling with how to animate the char ...

Angular2 (RC5) global variables across the application

I am seeking a solution to create a global variable that can be accessed across different Angular2 components and modules. I initially considered utilizing dependency injection in my `app.module` by setting a class with a property, but with the recent angu ...

CSS Navigation Bar Fails to Function Properly on Mobile Devices

Check out the plunkr I have created by visiting: http://plnkr.co/edit/gqtFoQ4x2ONnn1BfRmI9?p=preview The menu on this plunkr functions correctly on a desktop or laptop, but it doesn't display properly on a mobile device. I suspect that the issue may ...

Issues with ajax functionality

Utilizing Codeigniter When clicked ... .. insert some code.... $.ajax({ url: '<?php echo base _ url(); ?>locations/getmap', dataType: 'json', type: 'POST', data: location_data, success ...

Can an AJAX upload progress bar be implemented in Internet Explorer without using Flash?

I've been searching for solutions to upload files without using flash, but all of them either require flash or lack a progress bar on IE (7-8). I couldn't find any mention of an "progress" event in the MSDN documentation for XMLHTTPRequest. Is i ...

What is the best way to implement a custom toast delay in a React application using setTimeout

The concept is straightforward: When the function showToast is called, I aim to change my toast's className to show, and then remove it by replacing with an empty string after displaying it for 3 seconds. HTML: <div id="toast">New col ...

Leveraging IntersectionObserver to identify the video in view on the screen

Our Objective I aim to implement a swipe functionality for videos where the URL changes dynamically based on the ID of the currently displayed video. Challenges Faced Although I managed to achieve this with code, there is an issue where the screen flashe ...

What is the best way to create a sortable column that is based on a nested object within data.record?

I am utilizing jquery jtable to showcase some data in a table. I have been using the following code snippet for each field in the table to display the data and enable sorting: sorting: true, display: (data) => { return data.record.<whatever_value ...

Trigger an email notification in Google Sheets whenever a designated cell is populated

I am currently exploring ways to enhance collaboration in project management, especially when dealing with numerous stakeholders (Although there are free tools available, integrating new procedures into a public organization's projects can be quite ch ...

What is the best way to locate a user-provided string within word boundaries using JavaScript regex?

Employing JavaScript, I am currently searching a body of text. Users are given the option to input any string they desire, and then I aim to search for that specific string, ensuring it is considered a "whole word" located between boundaries. All I need i ...

Oops! Looks like we encountered a fatal error while running the unit tests. Unfortunately, the unit

After setting up the project environment and successfully installing grunt, I attempted to run grunt in the terminal which resulted in the following output: https://i.stack.imgur.com/4xnll.png I proceeded to follow the steps. Running grunt build --env ...

The backend error message isn't triggering an alert!

My current issue involves using jQuery to execute a .php file. Whenever an error occurs in the backend, I want to display an alert message with the error details. However, when intentionally submitting with an error, no alert pops up and it just proceeds t ...

Leveraging Mongodb's aggregate feature to calculate the overall quantity of a specific product that has been ordered across all

Recently delving into the realm of javascript, I am currently tackling a dashboard project that requires displaying the total quantity of each product ordered. Although I have successfully defined a variable 'qty' to represent the quantity of an ...

Activate jQuery after a vanilla JavaScript function has been executed

I have created a jQuery function that captures any changes made to an input field and stores them in a variable. The input field is connected to a vanilla JavaScript based API library, which I am unable to convert to jQuery. This library is for an address ...

I am not sure where the file is stored between selecting it for upload and actually uploading it

Currently, I am developing a web application using C#, Angular, and SQL. I am in the process of creating a feature that allows users to upload an Excel file into the database by clicking a button on the frontend. Upon clicking the button, a dialog box open ...

"Unleashing the power of plugins in your Nuxt.js store: A step

I am facing an issue with my gtag (analytics plugin) where I can access it on my components but not on my store. Any suggestions are welcome. Thank you. plugins/vue-gtag.js import Vue from "vue" import VueGtag from "vue-gtag" export d ...

The phenomenon of jQuery AJAX converting the escape character from %20 to + has been observed

I am encountering an issue with my jQuery AJAX function which is supposed to save an image path to the database. Below is an example parameter: var data = {}; data['url'] = "Path%20to%20URL"; Normally, if there is a space or %20, it sh ...

Constance in JavaScript

Looking to create constants in Javascript and seeking advice on the best way to do so. While I am aware that true constants don't technically exist, I'm finding it difficult to change values after exporting them. Are constants necessary? Is the ...