angularjs basic authentication headers

妖精的绣舞 提交于 2019-12-10 13:45:09

问题


I'm trying to connect to API and send authentication headers. I'm using Base64 as described here 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);
        });
}

but i'm getting this error

XMLHttpRequest cannot load https://.../Category.json?callback=?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://....com' is therefore not allowed access. The response had HTTP status code 400.


回答1:


Looks like you are running into issues with Cross-Origin Resource Sharing (CORS). Have a read of: How does Access-Control-Allow-Origin header work?

If you control the server then you can have it send back an appropriate Access-Control-Allow-Origin header as per what the server code in the question you referenced (How do I get basic auth working in angularjs?)



来源:https://stackoverflow.com/questions/27954024/angularjs-basic-authentication-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!