简体   繁体   English

未使用 Angular $http 设置 Content-Type 标头

[英]Content-Type header not being set with Angular $http

I'm trying to make a cross-origin POST request using Angular $http with the following code.我正在尝试使用 Angular $http 和以下代码发出跨域 POST 请求。

//I've tried setting and removing these http config options
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';

//Basic request, with some private headers removed
return $http({
    method: 'POST',
    //withCredentials:true,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    params: params,
    url: url
});

The preflight OPTIONS request gets a 200 OK , but the subsequent POST receives a 400 Bad Request response.预检 OPTIONS 请求获得200 OK ,但随后的 POST 收到400 Bad Request响应。 Looking at the trace in Chrome's debug window, I do not see a Content-Type: application/x-www-form-urlencoded; charset=UTF-8查看 Chrome 调试窗口中的跟踪,我没有看到Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 header for the POST. POST 的Content-Type: application/x-www-form-urlencoded; charset=UTF-8标头。 I assume this is why the the server is returning a Bad Request response.我认为这就是服务器返回错误请求响应的原因。

I'm setting some other custom headers that I have omitted form the code above, and they are being sent and displayed fine.我正在设置一些其他自定义标头,我在上面的代码中省略了这些标头,并且它们被发送和显示得很好。

I should also mention that I can make this request using the Advanced Rest Client app for Chrome and receive the correct response.我还应该提到,我可以使用 Chrome 的 Advanced Rest Client 应用程序发出此请求并收到正确的响应。 (An access token) (访问令牌)

I have also tried just doing a straight-up XMLHttpRequest(), but I get the same errors.我也尝试过直接执行 XMLHttpRequest(),但我遇到了同样的错误。

Any insight on why my Content-Type header is not being set?关于为什么没有设置我的 Content-Type 标头的任何见解?

I'm not sure the Content-Type header will be sent if you are NOT sending any data.如果您不发送任何数据,我不确定是否会发送Content-Type标头。 Add a data object and try it:添加data对象并尝试:

return $http({
    method: 'POST',
    //withCredentials:true,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    data: data,
    url: url
});

Also, usually with a post you use data instead of params (get).此外,通常在帖子中使用data而不是params (获取)。

You can also refer to this SO question that has some more info on how to transform the data if you need to: How can I post data as form data instead of a request payload?如果需要,您还可以参考这个 SO 问题,其中包含有关如何转换数据的更多信息: 如何将数据作为表单数据而不是请求有效负载发布?

The 'Content-Type' header is not being added to the request if the data property is undefined, you can send empty string as data "" for example.如果数据属性未定义,则不会将 'Content-Type' 标头添加到请求中,例如,您可以将空字符串作为数据 "" 发送。

 var req = {
                method: 'GET',
                url: '<your url here>',
                //headers: {
                //    'Content-Type': 'application/json;charset=utf-8'
                //},
                data: ""
            };
  $http(req);

I face the same issue, while setting the headers.我在设置标题时面临同样的问题。 we need to send data parameter in the $http request like this我们需要像这样在$http请求中发送数据参数

$http({
    method: 'POST',
    headers: { 'Content-Type': 'application/json},
    data: {},
    url: url
}); 

we can send blank data also.我们也可以发送空白数据。 it will work fine.它会正常工作。

My problem was that I was setting the data variable to an object instead of a string.我的问题是我将data变量设置为对象而不是字符串。

return $http({
    method: 'POST',
    //withCredentials:true,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    data: {'key1':'value1', 'key2':'value2'},
    url: url
});

Once I changed it to data:'key1=value1&key2=value2' it worked fine.一旦我将其更改为data:'key1=value1&key2=value2'它工作正常。 There was also a backslash in there that I had to manually put in the %5c code for.那里还有一个反斜杠,我必须手动输入%5c代码。

Than you very much "Liran B " your fix worked like a champ for me.... issue was bugging me from several hours....比你非常“Liran B”你的修复对我来说就像一个冠军......问题困扰了我几个小时......

var req = {
                method: 'GET',
                url: '<your url here>',
                //headers: {
                //    'Content-Type': 'application/json;charset=utf-8'
                //},
                data: ""
            };

  $http(req);

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

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