Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4797

Why does my Angular resource ignore the action-specific url?

$
0
0

I have the following Angular code:

var app = angular.module('app', ['ngResource']);app.factory('Post', function ($resource) {    return $resource('/post', {}, {        query: { method: 'GET', url: '/post/get/:id', params: { id: '@id' } },        save: { method: 'POST', url: '/post/addorupdate/:id', params: { id: '@id' } },        delete: { method: 'POST', url: '/post/delete/:id', params: { id: '@id' } }    });});app.controller('appController', function ($scope, Post) {    $scope.createPost = function () {        var post = new Post({            title: "Test post",            body: "Test body.<br/ >Yay!"        });        post.$save();    };});

The markup looks like this:

<body data-ng-app="app"><div data-ng-controller="appController"><button ng-click="createPost()">Create Post</button></div></body>

When I click the button it tries to do a POST to the server, but to the original URL and not the action-specific URL. The URL from the request looks like this:

http://localhost:8080/post?id=undefined

Why is it trying to POST to just /post? Also, any idea why id=undefined? In other examples it seems like ?id=blah just isn't included in the request if the ID isn't specified.


Viewing all articles
Browse latest Browse all 4797

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>