Angular code is failing to send a signal to the server following a $http.post request

I've been using angular for about a week now and I've been struggling to solve this issue. I have a service that wraps around $http because multiple controllers make calls to the same URL. On one particular page, there is a lot of server-side business logic happening on a post request, so I want to trigger a $route.reload(). It seems like it's working because after vm.saveItem() the controller is reinitialized, but the $http.get() call doesn't actually send the signal to fetch new data from the server. What am I missing?

  myModule.factory("itemRepository", function ($http) {

    var baseUrl = "/api/inventory";

    return {
      getLocations: function (itemName) {
        return $http({
          url: baseUrl + "/" + itemName + "/locators",
          method: "GET",
          cache: false
        });

      },
      addNewLocator: function (itemName, newLocator) {
        return $http.post(baseUrl + "/" + itemName + "/locators", newLocator);
      }
    };

  });




// itemEditorController.js

(function () {
  "use strict";

  angular.module("app-inventoryitems")
    .controller("itemEditorController", itemEditorController);

  function itemEditorController($routeParams, $route, itemRepository) {

    // initialize vars
    var vm = this;
    vm.itemName = $routeParams.itemName;
    vm.errorMessage = "";

    // Models
    vm.items = [];
    vm.isBusy = true;

    vm.newLocator = {};
    vm.newLocator.name = vm.itemName;

    // PROBLEM HERE, does not call server after post new data 
    // and $route.reload() was called
    itemRepository
      .getLocations(vm.itemName)
      .then(function (response) {
        angular.copy(response.data, vm.items);
      }, function (err) {
        vm.errorMessage = "Failed to load batches.";
      })
      .finally(function () {
        vm.isBusy = false;
      });

    vm.saveItem = function () {
      vm.isBusy = true;

      itemRepository.addNewLocator(vm.itemName, vm.newLocator)
        .then(function (response) {
          vm.newLocator = {};
          $route.reload();
        }, function (error) {
          vm.errorMessage = "Failed to save new item.";
        })
        .finally(function () {
          vm.isBusy = false;
        })
    }
  }
})();

Answer №1

Consider incorporating dynamic parameters into your request:

 $http({
      url: baseUrl + "/" + itemName + "/locators",
      method: "GET",
      cache: false,
      params: {
         r: Math.random().toString(16).substr(2)
      }
    });

If this resolves the issue, explore HTTP Caching policies.
Search for appropriate server-side headers to implement.

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

Mongoose and Node combination causing delays in database queries

While GET and DEL requests work well, I'm having trouble with POST. When I send a string to the database using POST, it doesn't show up on my web app right away. It only appears when I add a second string, almost like there is a delay? app.post ...

Leverage the Express JS .all() function to identify the specific HTTP method that was utilized

My next task involves creating an endpoint at /api that will blindly proxy requests and responses to a legacy RESTful API system built in Ruby and hosted on a different domain. This is just a temporary step to transition smoothly, so it needs to work seam ...

Node.js request body is not returning any data

UPDATE: @LawrenceCherone was able to solve the issue, it's (req, res, next) not (err, res, req) I'm in the process of building a MERN app (Mongo, Express, React, Node). I have some routes that are functioning well and fetching data from MongoDB. ...

Ajax is coming back with a value that is not defined

Currently, I am working on a search function that is responsible for searching a name from the database. When the user clicks "add", the selected item should appear below the search field so that it can be saved in the database. However, I am encountering ...

Retrieving a boolean value from a function that includes an asynchronous AJAX request

In my calendar application, I am working with an array of dates: <div v-for="date in dates">{{ date }}</div> I want to apply a conditional class based on the outcome of an isWeekend() method that involves making an API call: <div v-for="d ...

What is the best way to include a JavaScript variable within CSS styling?

I am currently implementing a dropdown menu system where the image of a parent div changes on mouse hover over a link, and reverts back when the mouse moves away. I have this functionality set up using a variable in my HTML code. Is there a way for me to m ...

Jquery Plugin fails to generate dynamic elements effectively

I developed a masking jQuery script that dynamically adds elements to an existing input element. var link = $('<a title="show" role="link" href="#" class="masker-value">show</a>'); wrapper: function() { container = $(container) ...

Tips for accessing arrayList data within a loop in JavaScript and displaying it in an HTML <c: forEach> tag

I have an array list stored inside a javascript code block. I am looking to extract this array list and iterate through it using the html tag <c:forEach>. How can I achieve this? Currently, I am able to display the array list using <h:outputText&g ...

What are the optimal scenarios for utilizing angular $q and how should it be

I've been trying to wrap my head around the functionality of Angular's $q, but it's just not clicking for me. Can someone explain when and how $q should be utilized in Angular? ...

Utilizing Vuejs to initiate a GET request using a method

Currently, I am working on enhancing the functionality of a GET request in Vue. My goal is to attach the request sending action to the form's submit button and also include a parameter from a text field within the form. HTML <html> <head> ...

Encountering Rendering Issues with EJS Post HTML Conversion

After working on a much larger project for over a month, I'm now six hours into troubleshooting and everything is starting to blend together. Prior to switching to EJS, everything was working perfectly. I'm not looking for someone to solve the ...

What is the process for incorporating multiple HTML pages into an Ionic hybrid mobile application?

Struggling to combine my sign in and sign up pages into a single index.html page. I'm currently working on a project involving Hybrid mobile app development. ...

Navigating through various JavaScript libraries with varying loading times can be a tricky task when working within the Angular

As I dive into learning Angular, I find myself pondering the architecture of my app. The project I'm embarking on will heavily rely on various external libraries such as jQuery, jQuery.ui, jsPlumb, and more, each with their own loading times. I unde ...

When working with Vue JS, consider utilizing a data or computed property that is dependent on the value of a particular prop. In this case, the prop being altered is named "

While using pagination, I encountered an issue where the nextPage method works fine but the previousPage method throws an error. The error message warns against directly mutating a prop as it gets overwritten whenever the parent component re-renders. It s ...

Update the page with AJAX-loaded content

I am currently utilizing a .load() script to update the content on a webpage in order to navigate through the site. This is resulting in URLs such as: www.123.com/front/#index www.123.com/front/#about www.123.com/front/#contact However, I am encountering ...

refreshing a seraph or seraph model with new values

I am looking to modify an object in a Neo4j database using Seraph. The code I have been using seems to always add a new object instead of updating the selected one. app.put('/contactlist/:name',function (req,res){ db.find({name: req.params.name ...

Error: It seems like there was a problem with the injector and the module could not be

Currently, I am encountering an issue with the code below. Whenever I try to run it, I receive the following error: Uncaught Error: [$injector:nomod]. Even though the module name is correct, I am unsure of why this error is occurring. I have double-checked ...

Issue with Tooltip Position when Scrolling Sidebar is causing display problems

My goal is to create a Sidebar with Tooltip attached to its <li> elements similar to this example: Screenshot - Good Tooltip However, I am facing an issue where the position of the Tooltip breaks when scrolling to the bottom of the sidebar, causing ...

Nested for loops are utilized in order to extract names from a JSON array

I am faced with a challenge involving a container that contains rows with 4 columns each. My goal is to retrieve the title of each column using ajax from a json array by utilizing a for loop. Initially, I successfully achieved this for one row using a sing ...

Tips for transforming a hover menu into a clickable menu

The scenario above demonstrates that when hovering over the dropdown menu, it displays a submenu. However, I want the submenu to be displayed after clicking on the dropdown text. Could anyone provide assistance on how to change the hovermenu to a clickabl ...