TL;DR 解决您的问题
您的代码中的问题可能是以下行:
Friends.mes({id: 'jb'}, {key : 'value'} ).$promise.then(function(data){
尝试将其更改为:
Friends.mes({id: 'jb', key: 'value'}).$promise.then(function(data){
完成此操作后,服务器路由上的 req.body 对象将包含以下 {id: 'jb', key: 'value'} 而不是 { '{"key":"value"}': '' }。
$resource 应用示例:
我创建了一个使用 $resource 获取和发布到 API 的小示例。
./public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular-resource.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="app" ng-controller="friendController as vm">
<div>
{{vm.friend}}
</div>
<button ng-click="vm.getFriend()">Get User</button>
<button ng-show="vm.friend" ng-click="vm.messageFriend(vm.friend)">Message Friend</button>
</body>
</html>
./public/script.js
angular.module('app', ['ngResource'])
.factory('friendFactory', ['$resource', function ($resource) {
return $resource(
'/api/v1/friends/:id',
{id: '@_id'},
{
mes: {
method: 'POST',
headers: {'content-Type': 'application/x-www-form-urlencoded'},
isArray : false,
params: { message: true }
}
}
);
}])
.controller('friendController', ['friendFactory', function (friendFactory) {
var vm = this;
vm.friend = null;
vm.getFriend = _getFriend;
vm.messageFriend = _messageFriend;
function _getFriend() {
friendFactory.get({id: 1})
.$promise
.then(function (res) {
console.log(res);
vm.friend = res;
});
}
function _messageFriend(friend) {
/* method 1: call $mes on a friend object: */
// vm.friend.$mes({message: 'A message'})
// .then(function (friend) {
// vm.friend = friend;
// });
/* method 2: call mes on the resource: */
friendFactory.mes({_id: 1, message: 'A message'})
.$promise
.then(function (friend) {
vm.friend = friend;
});
}
}]);
index.js - 托管页面并公开简单 api 的 node.js 服务器
var express = require('express')
, path = require('path')
, app = express()
, bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
app.use(express.static(path.join(__dirname, './public')));
var friends = [
{ _id: 0, name: 'A' },
{ _id: 1, name: 'B' },
{ _id: 2, name: 'C' },
{ _id: 3, name: 'D' }
];
app.get('/api/v1/friends', function (req, res) {
return res.send(friends);
});
app.get('/api/v1/friends/:id', function (req, res) {
for (var i = 0; i < friends.length; i++) {
if (friends[i]._id == req.params.id) return res.send(friends[i]);
}
return res.status(404).send('Not found');
});
app.post('/api/v1/friends/:id', function (req, res) {
for (var i = 0; i < friends.length; i++) {
if (friends[i]._id == req.params.id) {
friends[i].message = req.query.message;
return res.send(friends[i]);
}
}
return res.status(404).send('Not found');
});
app.listen(8080, function () {
console.log('Server running');
});
这个例子有一个简单的 API,可以让你
- 获取好友列表
- 结交一个朋友
- 向朋友发布消息(我假设这就是您的 mes 的含义?)。发布时 -
message 将作为查询字符串参数 (req.query.message) 或正文 (req.body.message) 使用。
在本例中,它简单地将“消息”附加到朋友并返回对象。
注意事项(在friendController):
默认情况下,在现有朋友对象上调用$mes会返回一个承诺,因此无需链接.$promise。
对资源本身调用mes不会返回承诺,因此需要$promise。