【发布时间】:2014-08-23 23:06:47
【问题描述】:
为了将文件作为表单的一部分发送,我将内容类型更改为“multipart/form-data”,但是当我检查控制台时,它仍在使用应用程序/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;
});
谷歌浏览器中的“检查网络请求”标签如下:
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, */*
【问题讨论】:
-
头参数不应该是第四个参数吗? github.com/mgonto/restangular#custom-methods 看到这个答案:stackoverflow.com/questions/24933574/…
标签: json angularjs html sails.js restangular