【问题标题】:angular $http headers not working角度 $http 标头不起作用
【发布时间】:2017-07-27 09:06:10
【问题描述】:

我的客户端是这样的:

$http(
       {
            method: 'GET',
            url: myConfig.serverUrl + '/getUserId',
            data: '',
            headers: { 'Authorization' : '21321313'},
       });

Node js 和 express 中的服务器端:

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
next();

});

但请求未设置授权值
OPTIONS /getUserId HTTP/1.1. Host: xxxxx. Connection: keep-alive. Pragma: no-cache. Cache-Control: no-cache. Access-Control-Request-Method: GET. Origin: http://localhost:9000. User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36. Access-Control-Request-Headers: authorization. Accept: */*. Referer: http://localhost:9000/. Accept-Encoding: gzip, deflate, sdch. Accept-Language: en-US,en;q=0.8,he;q=0.6.
我尝试了很多方法,但没有一个有效。 当我尝试与邮递员一起尝试时,我得到了价值,所以我认为我的问题在我的角度方面,但我无法弄清楚问题是什么。

【问题讨论】:

    标签: javascript angularjs node.js jwt


    【解决方案1】:

    服务器端的这个问题, 在 "OPTIONS" API 调用中无需检查授权

    【讨论】:

      【解决方案2】:

      您需要指定authorization type 类似'Authorization', 'Basic ZWx1c3VhcmlvOnlsYWNsYXZl'

      【讨论】:

        【解决方案3】:

        在基本身份验证中,base64 用于解码用户名和密码。所以,注入并使用Base64对用户名和密码进行编码

        var token = Base64.encode(username + ':' + password);
        $http.defaults.headers.common['Authorization'] = 'Basic ' + token;
        

        【讨论】:

          猜你喜欢
          • 2012-12-02
          • 1970-01-01
          • 1970-01-01
          • 2018-07-24
          • 2017-11-11
          • 1970-01-01
          • 2021-12-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多