【问题标题】:angularjs basic authentication headersangularjs 基本身份验证标头
【发布时间】:2015-03-13 06:58:50
【问题描述】:

我正在尝试连接到 API 并发送身份验证标头。 我正在使用这里描述的 Base64 How do I get basic auth working in angularjs?

function Cntrl($scope, $http, Base64) {

$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('----myusername----' + ':' + '----mypass---');
$http({method: 'GET', url: 'https://.../Category.json'}).
    success(function(data, status, headers, config) {
        console.log('success');
    }).
    error(function(data, status, headers, config) {
        alert(data);
        });
}

但是我收到了这个错误

XMLHttpRequest 无法加载https://.../Category.json?callback=?。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://....com' 不允许访问。响应的 HTTP 状态代码为 400。

【问题讨论】:

    标签: angularjs http-authentication angular-http-auth


    【解决方案1】:

    您似乎遇到了跨域资源共享 (CORS) 的问题。阅读: How does Access-Control-Allow-Origin header work?

    如果您控制服务器,那么您可以让它根据您引用的问题中的服务器代码 (How do I get basic auth working in angularjs?) 发回适当的 Access-Control-Allow-Origin 标头

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多