【问题标题】:Restangular won't change Http Content-TypeRestangular 不会改变 Http Content-Type
【发布时间】: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, */*

【问题讨论】:

标签: json angularjs html sails.js restangular


【解决方案1】:

正如上面评论中所说,根据文档,您应该将标题作为第四个参数传递。所以你的代码应该是

    $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;
    });

参考网址:https://github.com/mgonto/restangular#custom-methods

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 2012-07-07
    • 1970-01-01
    • 2012-09-11
    • 2022-06-25
    • 2013-06-09
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多