2014-09-29 49 views
0

我试图创建使用一个单独的服务器上的PHP API angularjs一个小应用程序继在线教程(无论在我的本地机器)

,我的代码下面。当$ save()被调用时,我在服务器上复制出$ _POST,它总是空的。然而,我的数据正在加载好,所以API连接是好的。

你能告诉我我要去哪里吗?

passwordServices.factory('Password', ['$resource', 
    function($resource){ 
     return $resource(api_base + 'passwords/index/', {}, { 
      query: {method:'GET', params:{}, isArray:true} 
     }); 
    } 
]); 

passwordControllers.controller('PasswordDetailCtrl', ['$scope', '$routeParams', '$http', 'State', 'Password', 
    function($scope, $routeParams, $http, State, Password) { 
     $scope.password = Password.get({passwordId: $routeParams.passwordId}, function(data) { 
      $scope.password.Password.title = "test"; 
      $scope.password.$save(); 
     }); 
    } 
]); 

及以下的情况下我的CakePHP API代码

public function index(){ 
     $this->autoRender = false; 
     $this->response->type('json'); 

     if ($this->request->is('post')){ 
      var_dump($_POST); 
     }else{ 
      if(isset($_GET['passwordId'])){ 
       $results = $this->Password->find('first', array(
        'conditions' => array(
         'Password.id' => $_GET['passwordId'] 
        ), 
        'contain' => array(
         'Provider', 
         'Type' 
        ) 
       )); 
      }else{ 
       $results = $this->Password->find('all', array(
        'contain' => array(
         'Provider', 
         'Type' 
        ) 
       )); 
      } 
      $this->response->body(json_encode($results)); 
     } 

    } 
+0

这是这个对不对? $ scope.password.Password.title =“test”; 不应该是$ scope.password.title =“test”; ? – 2014-09-29 16:21:28

+0

我使用cakephp作为API,它返回{Password:{title:'',..}} 我是否认为POST应该发布数据?或者是所有数据都应该通过URL? – user195257 2014-09-29 16:35:48

回答

0

试试这个

服务 -

passwordServices.factory('Password', function($resource, $q) { 
    var resource = $resource(api_base + 'passwords/index/', {}, { 
    query: { 
     method: 'GET', 
     isArray: true, 
     cache: false 
    }, 
    save: { 
     method: 'POST', 
     isArray: false, 
     cache: false 
    } 
}); 
return { 
    getPasswords: function() { 
     var deferred = $q.defer(); 
     resource.query({}, 
      function(response) { 
       deferred.resolve(response); 
      }, 
      function(response) { 
       deferred.reject(response); 
      } 
     ); 
     return deferred.promise; 
    }, 
    savePassword: function(someObj) { 
     var deferred = $q.defer(); 

     resource.save({}, someObj, 
      function(response) { 
       deferred.resolve(response); 
      }, 
      function(response) { 
       deferred.reject(response); 
      } 
     ); 

     return deferred.promise; 
    } 
}; 
]); 

控制器 -

passwordControllers.controller('PasswordDetailCtrl', ['$scope', '$routeParams', '$http', 'State', 'Password', 
    function($scope, $routeParams, $http, State, Password) { 

     $scope.handleGetPasswords = function (response) { 
      $scope.password = response; 
      $scope.password.Password.title = 'test'; 
      Password.savePassword($scope.password).then(function (response) { 
       console.log(response); 
      }) 
     }; 

     Password.getPasswords().then($scope.handleGetPasswords); 

    } 
]);