0
votes

Angularjs code

 var app = angular.module('myApp', []);
 app.factory('httpSend', ['$http', '$q', function($http, $q) {
     var app = {};
     app.sendToServer = function(data) {
         $http({
             method: "POST",
             url: '/report',
             data: data,
             headers: {
                 'Content-type': 'application/x-www-form.urlencoded;'
             }
         }).then(function(response) {
             debugger
             var result = data;
         });
     }
     app.getfromServer = function() {
         var def = $q.defer();
         $http.get('/report').then(function(data) {
                 console.log(data);
                 def.resolve(data);
             }),
             function(error) {
                 def.reject("Failed to get albums");
             };
         return def.promise;
     }
     return app;
 }]);
 app.controller('myCtrl', ['$scope', '$http', 'httpSend', '$filter', function($scope, $http, httpSend, $filter) {
     $scope.names = ["ankit patidar", "adhishi ahari", "kritin joshi", "kautilya bharadwaj", "punita ojha", "manvi agarwal", "apeksha purohit", "shipra jain", "mansi nangawat", "praveen soni"];
     $scope.data = [];
     $scope.names.forEach(function(name) {
         $scope.data.push({
             name: name,
             checkin: "",
             checkout: ""
         })
     });
     $scope.login = [];
     $scope.check = function(name, doing) {
         debugger
         name[doing] = new Date();
         name[doing] = $filter('date')(name[doing], 'dd-MM-yyyy hh:mm:ss');
         $scope.login.push(angular.copy(name));
         if (doing == "checkout") {
             var q = JSON.stringify($scope.login);
             httpSend.sendToServer(q);
         }
     }
     $scope.getData = function() {
         httpSend.getfromServer();
     }
 }]);

`

Python Code

 def get(self):
 logging.info('get is triggered')
 obj = CheckIn.query().fetch()
 emp_obj = []
 for x in obj:
     logging.info('I am inside for loop ')
 emp_obj.append({
     'name': x.name,
     'Check_in': x.inDate,
     'check_out': x.outDate
 })
 logging.info('I am inside emp_obj')
 self.response.write(json.dumps(emp_obj))

i need to fetch all the data stored on ndb datastore on front end view thats why i m using http get method but error is showed method not allowed. can u please help e despite using query fetch and showing its response on python ad triggering get method, why error is coming, is there a mistake in control flow or something is missing in my get method, as for now i m able to post nd store data

1
Don't use the var app = {}; for your factory logic and change x-www-form.urlencoded to json.Change the name of var app in ur factory logicVivz
instead of var app={} what to use??? and changing the content type also, no changes are there, showing the same errormanvi agarwal
You can use any other name. You are loading your angular module on var app. So its better if you use any other variable name for your factory logic.Vivz
thnks, it helped me to move atleast one step forward with a new errormanvi agarwal
Great, What error are you getting now? BTW var result = data; should be changed to var result = response.data;Vivz

1 Answers

0
votes

Change your factory to the following. Don't use the same variable app that you are using for initialising your module for your controller logic.

app.factory('httpSend',['$http', '$q',function($http, $q){
  return {
    'sendToServer': function(data) {
      var def = $q.defer();
        $http({
            method: "POST",
            url: '/report',
            data: data,
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
            debugger
            var result = response.data;
            def.resolve(result );
        });
          return def.promise;
    },
    'getfromServer': function() {
        var def = $q.defer();
        $http.get('/report').then(function(data) {
                console.log(data);
                def.resolve(data);
            }),
            function(error) {
                def.reject("Failed to get albums");
            };
        return def.promise;
    }
}
        }]);