sin*_*inθ 5 html5 json angularjs sails.js restangular
为了将文件作为表单的一部分发送,我将内容类型更改为"multipart/form-data",但是当我检查控制台时,它仍然使用application/json; charset = utf-8和Node.js(与Sails.js框架)打印错误,说它是无效的JSON.
这是代码:
$rootScope.objects.user.withHttpConfig({
transformRequest: angular.identity
}).customPUT($scope.objects.user, "", { // user object contains a file
'Content-Type': "multipart/form-data" // should change content-type. I've tried using `undefined` as well
}).then(function(resp) {
if (resp == "OK") {
$scope.successes = [{
msg: "Saved"
}];
}
}, function(err) {
console.log(err);
$scope.errors = err.data.errors;
});
Run Code Online (Sandbox Code Playgroud)
Google Chrome中的"检查网络请求"标签显示:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0
Referer: http://localhost:1337/user/settings
Host: localhost:1337
DNT: 1
Content-Type: application/json;charset=utf-8
Content-Length: 505679
Connection: keep-alive
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: application/json, text/plain, */*
Run Code Online (Sandbox Code Playgroud)
正如上面的评论和根据文档所述,您应该将标头作为第四个参数传递。所以你的代码应该是
$rootScope.objects.user.withHttpConfig({
transformRequest: angular.identity
}).customPUT(
$scope.objects.user,
undefined,
undefined,
{
'Content-Type': "multipart/form-data"
}
).then(function(resp) {
if (resp == "OK") {
$scope.successes = [{
msg: "Saved"
}];
}
}, function(err) {
console.log(err);
$scope.errors = err.data.errors;
});
Run Code Online (Sandbox Code Playgroud)
参考网址: https: //github.com/mgonto/restangular#custom-methods
| 归档时间: |
|
| 查看次数: |
1049 次 |
| 最近记录: |