javascriptNot able to post data angular ng-resource

I want to persist data using post method throught angular ngResource.
I have written the below code for the process. But, when I try to persist
error is coming.

JS CODE

angular.module('app').factory("testService", function($resource) {
return{
    addCount: $resource('/save/count',{method:'POST',params:{ date: '@date',
                    count: '@count'}}
        )
};
});

//controller code

$scope.add = function(){
testService.addCount().get({ date: new     Date(formattedDate).toString("MM/dd/yyyy"),
            count: count},function(response){
                console.log('success')

        });};

As soon as I click the add() from my html I am getting the below error.
TypeError: Cannot read property 'get' of undefined

Kindly,provide a suggestion to overcome this issue

RESTful services should be on a per entity basis. So you route should just be /Count or api/Count depends on how you set up your API endpoints.

Then when you use your $resource, just point it at your endpoint and let $resource handle posting etc.

The $resource follows the meanings behind the HTTP verbs. So GET should get some data, POST should save data, PUT should update, DELETE should delete etc.

$scope.add = function(){
    var Count = $resource('/count');
    Count.save({
         date: new Date(formattedDate).toString("MM/dd/yyyy"),
         count: count
    }, function() {
        // success
    });
};

That is how I would see myself using $resource. You can always abstract out the creation of Count to the service you created.


$resource

Link

So when you call $resource('some/route'); it will return an object containing these methods:

{ 'get':    {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

As you can see, save is the POST method. So to call it you take a reference to $resource('some/route'); like:

var Count = $resource('some/route');

Then you call a method.

For a HTTP GET "class" actions: Resource.action([parameters],
[success], [error])

For a non-GET "class" actions: Resource.action([parameters], postData,
[success], [error])

For a non-GET instance actions: instance.$action([parameters],
[success], [error])

So as I understand it, class means that reference Count, instance means when your class has been executed and you've recieved the object. This object is the instance

Related Articles
  • That might be strange but I need to specify some default POST data for my $resource using the factory method of the module. Does anyone have an idea of how to do that in AngularJS ? EDIT : Well, i want to do something like this : /** * Module declara
  • I'm trying to POST data in JSON format to a script I have running PHP on my webserver. I have found this post: How to send data to a website using httpPost, app crashes. Using the code he wrote (putting it on a separate thread first) I am able to pos
  • This may sound a bit strange but I need this to happen like this for a reason. I need python to be able to post data to a local PHP file on a linux server and then get a response from the PHP. I have tried this: p = subprocess.Popen("curl --data 'par
  • In a very basic test web app I am making, I am using angular to run a function when a form is submitted. The function asynchronously posts data to a simple api I built which is supposed to input data into a database dependent on the POST information
  • i am unable to get posted data on sever using php it returns Output : Array() Php Code public function create() { print_r($_POST); } Angular Code angular.module('index',[]) .controller('SignUpController', ['$scope','$http', function($scope,$http) { $
  • I have this bit of javascript: var jsonString = "some string of json"; $.post('proxy.php', { data : jsonString }, function(response) { var print = response; alert(print); and this bit of PHP (in proxy.php): $json = $_POST['json']; //set POST var
  • The following code will let me redirect the page to a new location, and take the GET variables with me.. <?php // set GET variables $name="John"; $email="[email protected]" // redirect the page header("Location: ../process.php?name=
  • Context: I have Cakephp v2.3 and I'm running XAMPP. My directory structure is thus: C:\xampp\htdocs\cakephp\app\webroot I have a .htaccess file located in ..\xampp\htdocs that redirects to cakephp (so basically I can type localhost and it will redire
  • I'm having some issues with calling an API. I need to send post data containing 2 things: an ID and an array of strings. I have tried a lot of things, all resulting in errors or simply not sending data in the right way. All answers I found, do not ha
You Might Also Like