AngularJs DELETE与数据集错误的内容类型头

前端之家收集整理的这篇文章主要介绍了AngularJs DELETE与数据集错误的内容类型头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个$http DELETE请求与一个有效载荷,但内容类型是错误的.我有一个数据对象,但是内容类型被设置为text / plain而不是application / json.从下面的代码和下面的网络请求可以看出,实际上是一个带有值的数据对象.有没有办法解决这个问题?非常感谢!

码:

$http({  
        method: "DELETE",url: ".../v2/places/" + place.id + "/locations/remove",data: location,headers: { Authorization: "Bearer " + rootServices.getToken() }  
    })

chrome网络请求摘要

Remote Address:54.83.54.37:443
URL:../v2/places/53b43e03e4b00cb25bcb16af/locations/remove
Request Method:DELETE
Status Code:500 Internal Server Error Request
Accept:application/json,text/plain,*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Authorization:Bearer …..
Connection:keep-alive
Content-Length:66
Content-Type:text/plain;charset=UTF-8
Host:sandBox….net
Origin:127.0.0.1:9000
Referer:127.0.0.1:9000/session
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML,like Gecko) Chrome/35.0.1916.153 Safari/537.36 Request
Payload: {room:Kitchen,appliance:Refrigerator,floor:Main
Floor}

您可以将标题中的内容类型与授权标题一起设置
$http({  
        method: "DELETE",headers: {'Content-Type': 'application/json',Authorization: "Bearer " + rootServices.getToken() }  
    })

或者您可以将其设置为广泛的:

module.config(function($httpProvider) {
  //$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'
  $httpProvider.defaults.headers.delete = { 'Content-Type' : 'application/json' };
});

文档状态:

Setting HTTP Headers The $http service will automatically add certain
HTTP headers to all requests. These defaults can be fully configured
by accessing the $httpProvider.defaults.headers configuration object,
which currently contains this default configuration:

$httpProvider.defaults.headers.common (headers that are common for all
requests): Accept: application/json,* / *
$httpProvider.defaults.headers.post: (header defaults for POST
requests) Content-Type: application/json
$httpProvider.defaults.headers.put (header defaults for PUT requests)
Content-Type: application/json To add or overwrite these defaults,
simply add or remove a property from these configuration objects. To
add headers for an HTTP method other than POST or PUT,simply add a
new object with the lowercased HTTP method name as the key,e.g.
`$httpProvider.defaults.headers.get = { ‘My-Header’ : ‘value’ }.

The defaults can also be set at runtime via the $http.defaults object
in the same fashion. For example:

https://docs.angularjs.org/api/ng/service/$http

猜你在找的Angularjs相关文章