简体   繁体   English

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

[英]AngularJs DELETE with data sets wrong content-type header

I am making a $http DELETE request with a payload but the content-type is wrong. 我正在使用有效负载制作$ http DELETE请求,但内容类型错误。 I have a data object, but the content-type is getting set to text/plain instead of application/json. 我有一个数据对象,但内容类型设置为text / plain而不是application / json。 As you can see from the code below and the network request below there is in fact a data object with values. 从下面的代码和下面的网络请求中可以看到,实际上有一个带有值的数据对象。 Is there a work around for this? 有没有解决这个问题? Much Thanks! 非常感谢!

code: 码:

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

chrome network request summary: chrome网络请求摘要:

Remote Address:54.83.54.37:443 远程地址:54.83.54.37:443
URL:../v2/places/53b43e03e4b00cb25bcb16af/locations/remove 网址:../ V2 /地区/ 53b43e03e4b00cb25bcb16af /位置/删除
Request Method:DELETE 请求方法:DELETE
Status Code:500 Internal Server Error Request 状态代码:500内部服务器错误请求
Accept:application/json, text/plain, */* 接受:application / json,text / plain, */*
Accept-Encoding:gzip,deflate,sdch 接受编码:gzip,放气,SDCH
Accept-Language:en-US,en;q=0.8 接受语言:EN-US,EN; Q = 0.8
Authorization:Bearer ..... Connection:keep-alive 授权:持票人.....连接:保持活力
Content-Length:66 内容长度:66
Content-Type:text/plain;charset=UTF-8 内容类型:文本/无格式;字符集= UTF-8
Host:sandbox....net 主持人:沙净....
Origin:127.0.0.1:9000 产地:127.0.0.1:9000
Referer:127.0.0.1:9000/session 的Referer:127.0.0.1:9000 /会话
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 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 (KHTML,与Gecko一样)Chrome / 35.0.1916.153 Safari / 537.36请求
Payload: {room:Kitchen, appliance:Refrigerator, floor:Main 有效载荷:{房间:厨房,电器:冰箱,地板:主要
Floor} 地板}

You can set the content type in the headers along with the Authorization header 您可以在标题中设置内容类型以及Authorization标头

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

Or you can set them app wide: 或者你可以在app app中设置它们:

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

The docs state: 文档说明:

Setting HTTP Headers The $http service will automatically add certain HTTP headers to all requests. 设置HTTP标头$ http服务将自动为所有请求添加某些HTTP标头。 These defaults can be fully configured by accessing the $httpProvider.defaults.headers configuration object, which currently contains this default configuration: 可以通过访问$ httpProvider.defaults.headers配置对象来完全配置这些​​默认值,该配置对象当前包含此默认配置:

$httpProvider.defaults.headers.common (headers that are common for all requests): Accept: application/json, text/plain, * / * $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. $ httpProvider.defaults.headers.common(所有请求通用的标头):Accept:application / json,text / plain,* / * $ httpProvider.defaults.headers.post :( POST请求的标头默认值)Content-Type :application / json $ httpProvider.defaults.headers.put(PUT请求的标头默认值)Content-Type:application / json要添加或覆盖这些默认值,只需在这些配置对象中添加或删除属性即可。 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, eg `$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }. 要为POST或PUT以外的HTTP方法添加标头,只需添加一个带有小写HTTP方法名称的新对象作为键,例如`$ httpProvider.defaults.headers.get = {'My-Header':'value'} 。

The defaults can also be set at runtime via the $http.defaults object in the same fashion. 也可以通过$ http.defaults对象以相同的方式在运行时设置默认值。 For example: 例如:

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

I found out the problem. 我发现了问题。 We need to type "content-type" instead of "Content-Type". 我们需要输入“content-type”而不是“Content-Type”。 If i understand it, the lower case override the previous. 如果我理解,小写覆盖前一个。

I will show an example: 我将举例说明:

$scope.deleteObject = function (object) {
    var objectId = object.id;
    var url = '/services/rest/deleteObject/' + objectId;
    var config = {
        headers: {
            'content-type': 'application/json'
        }
    };
    $http.delete(url, config)

    .then(function (response) {
        DO SOMETHING

    }, function (error) {
        DO SOMETHING
    });

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM