【问题标题】:How can I set the content-type as application/json for DELETE method in $resource AngularJS如何在 $resource AngularJS 中为 DELETE 方法设置内容类型为 application/json
【发布时间】:2015-11-10 21:05:59
【问题描述】:

我正在尝试将内容类型设置为 $resrouce 删除操作的 application/json。我需要对 application/json 强制执行 content-type 的唯一原因是 IE10,IE11 将 DELETE 请求的 content-type 检测为纯文本/文本,但所有其他浏览器和旧版本的 IE 将其检测为 application/json .我有一个来自后端的限制,它总是检查内容类型为 Json。

这是我试图执行它的方式:

remove: {
    method: 'DELETE', 
    headers: { 'Content-Type': 'application/json; charset=utf-8'}, 
    transformRequest: function(data, headerGetters) {
        headerGetters()['Content-Type'] = 'application/json';
        return angular.toJson(data); 
    }, 
update: {
     method: 'PUT'
}

但这仍然不起作用,无法将内容类型设置为 json。

【问题讨论】:

  • 如何检查这个问题:stackoverflow.com/questions/12785386/…
  • 你在没有 transformRequest 的情况下尝试过同样的方法吗?使用 $resource 服务有限制吗?
  • 我尝试不使用 transformRequest,但没有任何改变。仍然是 IE11 开发人员工具如何将内容类型作为纯文本而不是应用程序/json 传递。

标签: javascript json angularjs rest angular-resource


【解决方案1】:

TransformRequest 不是用来修改标头的。 HTTP 标头只能在使用 $http 时指定。

另见Angular $resource transformRequest doesn't change Request Header

【讨论】:

  • Anagular 1.4 应该支持 transformRequest 更改请求标头的能力,我使用的是最新版本。
猜你喜欢
  • 2023-03-06
  • 2012-02-28
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多