问题描述
我有一个前pressjs的API,我angularJS $资源对象交谈。我已经派人与邮差(用于测试REST API的铬工具)并在响应中的原始数据POST请求是:已提交
I have an expressjs api that my angularJS $resource objects talk to. I have sent a post request with postman (a chrome tool for testing REST apis) and the raw data in the response is: "submitted".
标头:
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
当我在角登出我的反应,我得到如下:
When I log out my response in angular I get the following:
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
我的前preSS code:
My express code:
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')
}
});
};
AngularJs厂:
AngularJs factory:
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
});
我的控制器:
$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)
});
};
为什么分手人物和解析响应作为一个数组,是它与我的头,angularjs或前preSS的问题?
Why is it breaking up the characters and parsing the response as an array, is it an issue with my headers, angularjs or express?
谢谢!
编辑:res.json有同样的问题。
res.json had the same problem.
推荐答案
在角资源有选项来包装响应 transformResponse
,应该解决这一问题。
in angular resource there is option to wrap the response transformResponse
, that should solve the issue
$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};
}
}
});
这篇关于AngularJS $资源响应返回从防爆pressJS字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!