2013-11-21 35 views
0

我想習慣$資源和它的所有榮耀,我無法設法做一個簡單的帖子吧。

給我使用$ http很簡單。

我可以做一個POST是這樣的:

$http.post("authorize/claim", collection).success(function() { 

發送收集作爲原料的對象或數組作爲有效載荷。

或像這樣:

$http.post("authorize/claim?claimItem="+collection).success(function() { 

我在哪裏發送集合作爲查詢字符串。

現在,當我嘗試使用$ resource做一個簡單的帖子時,數據總是作爲查詢字符串發送。我怎樣才能發送它作爲數組或對象?

這是我在做什麼。

var authoClaims = $resource('authorize/claim'); 

authoClaims.save(collection); 

回答

4

試試這個:

的index.html

<!DOCTYPE html> 
<html ng-app="myApp"> 

<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-resource.min.js"></script> 
    <script type="text/javascript" src="script.js"></script> 
</head> 

<body> 
    <div ng-controller="AuthorizeController"> 
    <form name="authorizeForm" novalidate> 
     <label>Login</label> 
     <input type="text" name="login" ng-model="authorizeClaim.login"> 
     <br/> 
     <label>Code</label> 
     <input type="text" name="code" ng-model="authorizeClaim.code"> 
     <br/> 
     <button ng-click="doAuthorizeClaim()">Authorize claim</button> 
    </form> 
    </div> 
</body> 

</html> 

的script.js

var myApp = angular.module('myApp', ['ngResource', 'myAppServices']); 

myApp.controller('AuthorizeController', ['$scope', 'Authorize', 
    function($scope, Authorize) { 

    $scope.doAuthorizeClaim = function() { 
     Authorize.save($scope.authorizeClaim, function() { 
     alert('Authorize claim saved'); 
     }); 
    }; 
    } 
]); 

var myAppServices = angular.module('myAppServices', ['ngResource']); 

myAppServices.factory('Authorize', ['$resource', 
    function($resource) { 
    return $resource('/api/authClaims', {}, {}); 
    } 
]); 

Plunker example

如果運行這個例子,你可以在網絡看到(德veloper工具/ Firebug)這樣的事情:

Request URL: http://run.plnkr.co/api/authClaims 
Request Method: POST 
Status Code: 404 Not Found 
Request Payloadview source 
{login:test1, code:code1} 

所以方法POST的作品。

相關問題