Issues with Angularjs $http.get functionality

On the server side of my ASP.net MVC application, I have a method that looks like this:

    [HttpGet]
    public JsonResult GetTrenings(string treningId)
    {
        var tempId = Guid.Parse(treningId);
        var trening = TreningService.GetTreningById(tempId);
        _trenings = TreningService.GetAllTreningsForUser(trening.UserId);
        return Json(_trenings, JsonRequestBehavior.AllowGet);

    }

I also have an Angular service :

publicApp.angularModule.factory('feedSingleTreningService', function ($q, $http) {

return {
   getTrenings: function (data) {
       var input = $http.get("/Feed/GetTrenings", { params: { treningId: data } });


        var deferred = $q.defer();

        deferred.resolve(input);
        return deferred.promise;
    },

};
});

And in my Controller, I call this service in this way:

 feedSingleTreningService.getTrenings(data).then(function(results) {
        console.log("test", results);
    });

However, nothing is shown in the console. I've debugged the server-side code and confirmed that the request reaches it and returns _trenings. The service also returns a promise, but nothing seems to happen.

I tried changing "then" to "finally", and while "test" was displayed in the console, the results were undefined.

What could be causing this issue?

Answer №1

There is no need to delay your call to $http because it already provides a promise.

Simply do the following:

return $http.get("/Feed/GetTrenings", { params: { treningId: data } });

This way, any function calling your function can use:

getTrenings(myData).then(function(data) { //do something }).fail(function() { //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

Creating Browser Extensions with Vue.js and Vue CLI

I am in the process of creating a Chrome Extension with a frontend powered by Vue.js. Everything was going smoothly using vuecli until my app started utilizing the Webextension-API. This API is only accessible to registered Extensions, not normal websites. ...

How to efficiently load 3000 objects in Openlayers using IE8

One of my clients exclusively uses IE8 and is working with an openlayers map. He is trying to load 3000 polygons onto the map, which Chrome and IE9 can handle without any issues. However, IE8 freezes when attempting to load the objects. These objects are c ...

Utilizing @casl/vue in conjunction with pinia: A guide to integrating these

I'm currently facing an issue with integrating @casl/ability and Vue 3 with Pinia. I'm unsure of how to make it work seamlessly. Here is a snippet from my app.js: import { createApp } from "vue" const app = createApp({}) // pinetree i ...

The variable that contains a function is invoked, but the result is undefined

I'm having trouble with a function that should return John temp (2021) when called, but instead it's returning John undefined (undefined) const user = { username : 'John', type: 'temp', yearJoin: 2021, user ...

Less-middleware in Node.js not automatically compiling files

I've incorporated the less-middleware into my Node.js Express app, but I'm encountering an issue. Whenever I update my screen.less file, it doesn't recompile automatically. To trigger a recompilation, I have to delete the generated .css file ...

CSS positioning with absolute value + determine the number of elements positioned above

Is it feasible to adjust the position of an absolutely positioned element based on the height of the element above it? The objective is to align elements with the class "pgafu-post-categories" one line above an H2 heading, even when the lengths v ...

Experience the power of TypeScript in a serverless environment as you transform your code from

I have some JavaScript code that needs to be converted to TypeScript. Currently, I have two files: API_Responses.js const Responses = { _200(data = {}) { return { statusCode: 200, body: JSON.stringify(data), }; } }; module.export ...

React component failing to update after receiving response from server for a specific room using socket.io-client with Python backend

I've developed a server backend in Python with Flask-SocketIO that includes a room feature for private conversations. When a user joins a room, the server triggers a function to inform the frontend where to direct messages to a specific user: socketio ...

Problem: Repeated attempts to open the popup are unsuccessful

Developed a leaflet map featuring markers that open popups when clicked. Also integrated a search bar for marker searchability. However, encountering an issue where each popup can only be opened once and on selecting a marker name, the zoom functionality w ...

The average duration for each API request is consistently recorded at 21 seconds

It's taking 21 seconds per request for snippet.json and images, causing my widget to load in 42 seconds consistently. That just doesn't seem right. Check out this code snippet below: <script type="text/javascript"> function fetchJSONFil ...

Common JavaScript Framework Startup Errors

Currently, I am delving into the world of JavaScript and experimenting with various thingamajigs. Could someone kindly shed some light on why my script is throwing errors? // Effects object var effects = { // Display an object show : function(obj) { o ...

How can you extract elements from a JSON array into separate variables based on a specific property value within each element?

In the following JSON array, each item has a category property that determines its grouping. I need to split this array into separate JSON arrays based on the category property of each item. The goal is to extract all items with the category set to person ...

The Nuxt.js landing page is having trouble loading, but all other components are functioning perfectly

I successfully created a blog using Nuxtjs and everything was working perfectly in development mode. However, when I deployed the application to my cPanel server, the landing page (index.vue) at showed an error message saying This page could not be found ...

Is there a way to minimize superfluous re-renders in React without resorting to the useMemo hook?

I am currently evaluating whether I should adjust my strategy for rendering components. My current approach heavily relies on using modals, which leads to unnecessary re-renders when toggling their visibility. Here is a general overview of how my componen ...

What is the method for changing the Uint8Array object into a string?

I am encountering a similar issue as the one discussed in this post. I have read the solution provided, but I am having trouble grasping how to implement it. Are there any alternative suggestions? Here is my code snippet: var eccrypto = require("eccrypto ...

Angular 2 TypeScript: Accelerating the Increment Number Speed

I'm working with a function in Angular 4 that is triggered when the arrow down key is pressed. Each time the arrow down key is hit, the counter increments by 1. In this function, I need to run another function if the counter reaches a certain speed. ...

Using jQuery to encapsulate HTML within a div tag

I am looking to insert some HTML markup into my webpage using jQuery. I want to wrap the highlighted yellow code below with <div class="section">...</div>. Here is an example of what I need: <div class="section"> <div>...< ...

I have the ability to showcase information from a JSON file using AngularJS, although it may not be in the exact manner that

I am currently working with AngularJS 1.6.4 and NeDB. When attempting to retrieve all users from the database using NeDB, it returns a JSON file containing information for every user. I then tried to display this data using AngularJS, but when using ng-re ...

How to position footer at the bottom of Material UI cards - see example below

After getting inspiration from the material-ui example of cards, I wanted to create a grid layout with multiple cards. My goal was to make all the cards have equal height (which I achieved using height:100%) and position the footer at the bottom of each ca ...

Is there a way to prevent ng-template-loader from scanning image src's?

Currently, I am beginning to incorporate webpack into my development workflow for an angular project. To create my templateCache, I have had to include the ng-template-loader. Below is a snippet of my webpack configuration: { test: /\.html$/, loa ...