Mel*_*991 6 javascript express angularjs
我有一个表达我的angularJS $资源对象的expressjs api.我已经发送了postman(一个用于测试REST apis的chrome工具)的帖子请求,响应中的原始数据是:"提交".
标题:
Connection ?keep-alive
Content-Length ?9
Content-Type ?text/html; charset=utf-8
Date ?Sun, 02 Feb 2014 12:02:20 GMT
X-Powered-By ?Express
Run Code Online (Sandbox Code Playgroud)
当我以角度注销我的回答时,我得到以下内容:
Resource
0: "S"
1: "u"
2: "b"
3: "m"
4: "i"
5: "t"
6: "t"
7: "e"
8: "d"
$promise: undefined
$resolved: true
__proto__: Resource
Run Code Online (Sandbox Code Playgroud)
我的快递代码:
exports.create = function(req, res) {
new Product(req.body).save(function(err) {
if (err) {
res.send('There was an error: ' + err);
}
else {
res.send('Submitted')
}
});
};
Run Code Online (Sandbox Code Playgroud)
AngularJs工厂:
pantherServices.factory('Product', function($resource, Defaults) {
var Product = $resource(Defaults.api_url + 'products', {productId: '@productId'} , {
find: {
method: 'GET',
url: Defaults.api_url + 'products/:productId',
},
all: {
method: 'GET',
isArray: true
}
});
return Product
});
Run Code Online (Sandbox Code Playgroud)
我的控制器:
$scope.newProduct = {
name: null,
description: null,
price: null,
display_price: null,
date_available: null
};
$scope.addNewProduct = function() {
var newProduct = new Product($scope.newProduct);
newProduct.$save(function(response, headers) {
console.log(response)
});
};
Run Code Online (Sandbox Code Playgroud)
为什么它会分解字符并将响应解析为数组,这是我的标题,angularjs还是表达式的问题?
谢谢!
编辑:res.json有同样的问题.
在角度资源中,可以选择包装响应transformResponse,以解决问题
$resource(appConfig.apiBaseUrl + '/newsletter/:id', {}, {
update: {method: 'PUT'},
weekly: {
method: 'POST', params: {id: 'weekly'}, transformResponse: function (response) {
// whatever you want to do
return {html: response};
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2336 次 |
| 最近记录: |