The function(result) is triggered when an http.get request is made

Can anyone help me figure out why my function is jumping after completing the request? It seems to be skipping over .then(function(result){ }.

I suspect that the issue might be related to the <a> element with an onclick attribute containing an href attribute.

Has anyone encountered this problem before?

var app = angular.module('devicesFromGroup', ['ngResource']);

var myInjector = angular.injector(["ng"]);
var $http = myInjector.get("$http");

function funcb($http){
    console.log("OLIEIEIEIEI");
    $http.get('http://localhost:8080/api/stuff/2')
    .then(function(result) {
        console.log("it's not printed");
    });
}

function funcC(id){
    myInjector.invoke(funcb);
    return true;
};

In another section of my JavaScript:

var a = document.createElement("a");
a.setAttribute('href',"http://localhost:8080/DevicesFromGroup.html");
a.setAttribute('onclick',"funcC(id);");

Answer №1

In the event that the call results in an error, the block is only invoked for success. Below, a catch block has been added to display any errors in the console.

function fetchData($http){
        console.log("OLIEIEIEIEI");
        $http.get('http://localhost:8080/api/data/2')
        .then(function(response) {

            console.log("This line will not be printed");


        })  
       .catch(function (error) {
          console.log("An error occurred: "+error);
       });
    }

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

Keep only certain fields and eliminate the rest

Write a function that filters out all fields except 'firstName' and 'lastName' from the objects. Check out this code snippet I came up with. Any feedback? let people = [ { firstName: 'John', lastName: &apo ...

Page not reaching the very top when scrolled

Having a puzzling issue that's got me stumped. Currently working on my professor's website at jurgec.net. The problem is specific to the mobile version of the site. Whenever clicking on a link like "I am an undergraduate student," scrolling down ...

Step-by-step guide on programmatically activating a radio button

I am working with a radio button and input field. I need the ability to programmatically toggle the radio button so that when this.iAreaOfCoverageForThresholdPasser.average-height is set to true, the radio button appears highlighted. Snippet of HTML: < ...

AngularJS orderby function organizes both numerical values and dates by treating them as strings

I am in need of creating a dynamic table using angular js to present data from a third party source that cannot be modified. The data to be displayed is completely unpredictable, meaning the number or order of columns can change at any time. The content sh ...

The discord.js argument for startsWith should not be a standard regular expression

What could be the reason behind this not working as expected? I am trying to check if a string starts with a number, and furthermore, I want it to handle multiple numbers. For instance, if the string starts with 1259823 then execute this code. I assume t ...

Exploring the loading of stateful data in ReactJS and implementing optimizations

Exploring the world of ReactJS in conjunction with Django Rest Framework (DRF) def MyModel(model): ... status = ChoiceField(['new', 'in progress', 'completed'...]) In my application, I have dedicated sections for eac ...

Troubles encountered with example code: Nested class in an exported class - Integrating Auth0 with React and Node.js

I am currently attempting to execute tutorial code in order to create an authentication server within my React project. Below is the code snippet provided for me to run: // src/Auth/Auth.js const auth0 = require('auth0-js'); class Auth { co ...

Using Node.js to display the outcome of an SQL query

I have been attempting to execute a select query from the database and display the results. However, although I can see the result in the console, it does not appear on the index page as expected. Additionally, there seems to be an issue with the way the r ...

jQuery Toggle and Change Image Src Attribute Issue

After researching and modifying a show/hide jQuery code I discovered, everything is functioning correctly except for the HTML img attribute not being replaced when clicked on. The jQuery code I am using: <script> $(document).ready(function() { ...

Optimal method for retrieving data from asynchronous functions in JavaScript

Currently, I am using the twit library for nodejs which has async calls. In my code, I have created functions like the following: function getUserFromSearch(phrase) { T.get('search/tweets', { q: phrase+' lang:pt', count: 1 }, funct ...

Update to the viewport meta tag in iOS 7

Anyone experiencing problems with the viewport tag after updating to iOS7? I've noticed a white margin on the right side of some sites. Adjusting the initial scale to 0.1 fixed it for iPhone but made it tiny on iPad 3, which is expected due to the low ...

How can you personalize the background color of a Material-UI tooltip?

Is there a way to customize the hover tooltip with a "? icon" for users providing input guidance in a text field? I prefer the design to have white background with grey text and larger font size, as opposed to MUI's default style which is grey with wh ...

Exploring MongoDB's Aggregation Framework: Finding the Mean

Is there a way to use the Aggregation Framework in MongoDB to calculate the average price for a specific Model within a given date range? Model var PriceSchema = new Schema({ price: { type: Number, required: true }, date: { ...

Editing input within a Bootstrap 4 popover causes it to lose focus

I am using Bootstrap 4 along with the Bootstrap colorpicker to implement a colorpicker within a popup that includes an input field for setting the color code. However, I am facing an issue where the input field (#color-value) seems uneditable when the popo ...

Differences in characteristics of Javascript and Python

As I tackle an exam question involving the calculation of delta for put and call options using the Black and Scholes formula, I stumbled upon a helpful website . Upon inspecting their code, I discovered this specific function: getDelta: function(spot, str ...

In the realm of Laravel, Vue, and Javascript, one may question: what is the best approach to omitting a key

When working with JSON data, I encountered a problem where leaving some keys unfilled resulted in incorrect results. I want to find a way to skip these keys if they are not filled. I have shared my code for both the backend and frontend below. Backend La ...

Issue with parameter functionality not working as expected

This code snippet is not functioning as expected. I am trying to extract and print the values from the URL parameter file:///C:/Users/laddi/Desktop/new%201.html?t=vindu&b=thind function GetURLParameterValue(param) { var pageURL = window. ...

Create a vibrant PNG image background that changes dynamically based on the dominant color of the

Is it possible to dynamically set the background color of a PNG image, either white or black, based on the dominant color in the image? For example, this image should have a dark background: https://i.stack.imgur.com/cerjn.png And this one should have a ...

Having issues with retrieving data using findOne or findById in Express and Node JS, receiving undefined values

Currently, I am working on a microservice dedicated to sending random OTP codes via email. Below is the code for my findbyattr endpoint: router.get('/findbyattr/:email', async (request, response) =>{ try { let requestEmail = reque ...

Achieving closure by fulfilling the previously fulfilled promise once more

I have a situation while using $q in AngularJS. If I create a single promise that is already resolved, is it possible to resolve it again? I am unsure if this is feasible, but if not, is there any method by which I can resolve the same promise repeatedly ...