我正在尝试连接到API并发送身份验证标头.
我正在使用这里描述的Base64 How do I get basic auth working in angularjs?
我正在使用这里描述的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?
How does Access-Control-Allow-Origin header work?
如果您控制服务器,那么您可以让它根据您引用的问题中的服务器代码发送回适当的Access-Control-Allow-Origin标头(How do I get basic auth working in angularjs?)