angularjs基本身份验证标头

前端之家收集整理的这篇文章主要介绍了angularjs基本身份验证标头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试连接到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,config) {
        alert(data);
        });
}

但是我收到了这个错误

XMLHttpRequest无法加载https://…/Category.json?callback=?请求的资源上不存在“Access-Control-Allow-Origin”标头.因此不允许原点’http://….com‘进入.响应具有HTTP状态代码400.

看起来您遇到了跨源资源共享(CORS)的问题.阅读:
How does Access-Control-Allow-Origin header work?

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

猜你在找的Angularjs相关文章