【问题标题】:Header is not setting in angularjs post request标头未在 angularjs 发布请求中设置
【发布时间】:2018-12-08 12:19:12
【问题描述】:

这是我的控制器,我在请求中添加了标头,但我的请求中没有添加标头。

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http) {
  $scope.call = function() {
    $http({
      method: 'POST',
      url: 'http://localhost:8081/api/GetData',
      headers: {
        'token': '33c028f2-6eb6-11e8-ac8b',
        'user': 'admin',
        'Content-Type': 'application/json'
      }
    }).then(function(response) {
        //$scope.names = response.data;
        console.log(response.data);
      },
      function(response) {
        //  alert(response.data);
        //Second function handles error
        $scope.content = "Something went wrong";
      }
    );
  }
});

当我检查我的浏览器控制台时,它没有在请求中显示任何标题。

响应标头 (138 B) 允许 POST、GET、OPTIONS、HEAD 内容长度
0 日期 2018 年 6 月 29 日星期五 10:10:58 GMT 服务器码头 (8.1.15.v20140411)

请求标头 (438 B) 接受
text/html,application/xhtml+xm…复制/xml;q=0.9,/;q=0.8
Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5
访问控制请求标头内容、令牌、用户
访问控制请求方法 POST 连接保持活动主机
localhost:8081 Origin null User-Agent Mozilla/5.0 (X11; Ubuntu;
Linu…) Gecko/20100101 Firefox/60.0

请帮助我了解为什么我的请求中没有设置标头

【问题讨论】:

  • 您使用的是哪个版本的 Angular?
  • 确保服务器接受 CORS 策略。如果没有,则永远不会发送标头

标签: angularjs


【解决方案1】:

确保您的 cors 已配置,根据 angular1 文档,您应该能够像这样设置标题:

     var req = {
     method: 'POST',
     url: 'http://example.com',
     headers: {
        'Content-Type': undefined
     },
     data: { test: 'test' }
     }

     $http(req).then(function(){...}, function(){...});

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2014-09-26
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多