Modifying the URL for POST and GET requests in NgResource

I have two different URLs for serving POST and GET methods.

- This URL uses the POST method and requires a request body with no parameters.

- It utilizes the GET method with input provided through parameters.

In my services.js file, I am using the following code for the POST call:

.factory('User', ['$resource',function($resource){
    return $resource('http://205.147.99.162:8080/UASAPI/UserServices');
}])

To make a POST call, I use the following code snippet which works as expected:

User.save(angular.toJson(userAndAttributesValues), function(res){},function(res){});

Now, how can I add the GET method URL inside the User factory to be able to use the get() and query() methods from ngResource?

How should I go about invoking it?

Answer №1

To clarify, it seems like you can configure your factory to provide both functions:

.factory('User', ['$resource', function($resource){
    return {
      sendData: function() {
        return $resource('http://205.147.99.162:8080/UASAPI/UserServices');
      },
      receiveData: function() {
        return $resource('http://1.2.3.4:1234/UASAPI/UserServices/9959116790/123456');
      }      
    }
}])

You can then utilize them according to your requirements:

User.sendData()
User.receiveData()

Answer №2

If you want to pass parameters to ngResource...

Here is how you can call it:

// Fetching data
$scope.posts = Post.get({param1:1, param2:2});

// Saving some data
Post.save()

To set up the resource, follow these steps:

angular.module('app',['ngResource'])
  .controller('MainCtrl', function($scope, Post) {

    $scope.post = new Post();
    $scope.posts = Post.get({param1:1, param2:2});
    Post.save()

  }).provider('Post', function() {
    this.$get = ['$resource', function($resource) {
      var API = $resource('http://205.147.99.162:8080/UASAPI/UserServices/:param1/:param2', {
        param1: "@param1",
        param2: "@param2"
      });

      return API;
    }];
  });

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

Improving Page Load Speed with HTML Caching: Strategies for Enhancing Performance when over half of the data transferred is for navigation menus

I manage a complex and expansive website that contains a significant amount of repetitive HTML elements such as the navigation menu and top ribbon. Loading a single page on my site can be resource-intensive, with up to 300KB of data required, half of whic ...

Having trouble resolving the FancyBox Jquery conflict and unable to find a solution

Attempting to implement FancyBox on a website. The demo is functional, proving its capability. Yet, encountering an error when trying to integrate it within the WordPress theme: Uncaught TypeError: $(...).fancybox is not a function The header file cont ...

What is the best way to create rotational movement for an object in a-frame?

Is there a way to have my entity rotate/spin on the Y-axis? I attempted the following: <a-entity position="-1 0.5 3" animation="property: rotation; to: 0 0 0 ; loop: true; elasticity:1000; dur: 10000" rotation="90 0 0"> <a-box position="1 0 ...

Issue with VueJS not functioning properly upon initial interaction or initial trigger

After some trial and error, I was able to successfully create dynamic elements. The main goal of this exercise is to generate dynamic divs based on a specified "count", with the ability to add multiple textboxes inside each div. If you're curious to ...

Is it possible to determine if a JavaScript/jQuery effect or plugin will function on iPhone and iPad without actually owning those devices?

Is there a way to ensure that a javascript/jquery effect or plugin that works well on all desktop browsers will also work on iPhone and iPad without actually owning those devices? Can I rely on testing it on the latest Safari for Windows to guarantee comp ...

Having trouble with ES6 in Canvas - why won't my code display correctly?

I'm currently working on developing a painting app using ES6. However, I'm facing issues with the positioning and line drawing on the canvas. The lines are not being drawn in the correct position; for example, the top-left corner is formed when ...

When you hover over the button, it seamlessly transitions to a

Previously, my button component was styled like this and it functioned properly: <Button component={Link} to={link} style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', height: 48, padding: '0 ...

utilize Angular's interface-calling capabilities

In the backend, I have an interface structured like this: export interface DailyGoal extends Base { date: Date; percentage: number; } Now, I am attempting to reference that in my component.ts file import { DailyGoal } from '../../interfaces' ...

Main.js in Electron cannot find the NODE_ENV environment variable

Currently, I am in the process of developing an application which involves: React 16.4.0 Electron 2.0.2 Webpack 4.11.0 After successfully compiling and running the app using webpack (webpack dev server), my focus now shifts to showing only Chr ...

What is the best way to ensure there is only one space after every 4 characters in a string?

Looking for a way to make social security number input more readable by inserting a whitespace after the first 4 characters in the string. The social security number has a total of 10 numbers, so the desired format is: 1234 567890. Most solutions I found ...

Preventing PCs from accessing a specific webpage: A step-by-step guide

I am currently using the code below to block phones: if { /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i .test(navigator.userAgent)) { window.location = "www.zentriamc.com/teachers/error ...

My Redux reducer is being overridden by changes made using setState or .map() within my React component

After logging this.state.orginalSeries, this.props.demandGraphSeriesDataReducer, and newSeries, it appears that all data is being mutated. Despite attempting to use .map() and .slice() to prevent mutation of the original reducer data, it seems to still be ...

When using Next.js, an error may occur when trying to use DOMPurify.sanitize(), displaying a TypeError message saying that dompurify__WEBPACK_IMPORTED_MODULE_6___default

Utilizing DOMPurify.sanitize() within dangerouslySetInnerHTML={{}} to render the innerHtml retrieved from the database. Initially, I'm employing getServersideProps() alongside next-redux-wrapper for this specific page. Installed dompurify using: npm ...

Images in SCSS distorted due to styling issues

I am encountering an issue with a round image displaying properly on a browser, but appearing distorted after building the apk and running it on my android phone. The image's width is greater than its height. How can I ensure that it remains round? M ...

Detect the number of times the button has been clicked without the reliance on a global variable

Is there a way to track the number of times a form submit button has been clicked using jQuery? I've come across some solutions here, but I'm looking for a method that doesn't involve using a global variable. Is it possible to achieve this ...

If every object within the array contains a value in the specified property, then return true

In my current project, I am working with an array of objects that looks something like this: $scope.objectArray = [ {Title: 'object1', Description: 'lorem', Value: 57}, {Title: 'object2', Description: 'ipsum', V ...

Encountered a 404 error while trying to install body-parser

Hey there, I'm new to this and ran into an issue while trying to install the body-parser package. Can you please guide me on how to resolve this problem? D:\Calculator>npm install body-paeser npm ERR! code E404 npm ERR! 404 Not Found - GET htt ...

The attempt to recover the "NetworkStatus" plugin from config.xml was unsuccessful. To resolve this issue, consider re-adding it. Error: npm: Command terminated with exit code 1

I'm encountering an issue while attempting to create an Android APK using Ionic. The error message I am receiving is as follows: Discovered saved plugin "NetworkStatus". Adding it to the project Failed to restore plugin "NetworkStatus" from config.xm ...

Display solely the error message contained within the error object when utilizing the fetch API in JavaScript

I am facing an issue in logging the error message to the console while everything else seems to be working fine. Specifically, I am unable to log only the message such as "email exists" when the email already exists in the system. const submitHandler = a ...

What is the best way to use res.sendFile() to serve a file from a separate directory in an Express.js web application?

I have a situation within the controllers folder: //controler.js exports.serve_sitemap = (req, res) => { res.sendFile("../../sitemap.xml"); // or // res.send(__dirname + "./sitemap.xml") // But both options are not working }; ...