angularjs基本认证头 [英] angularjs basic authentication headers

查看:274
本文介绍了angularjs基本认证头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到API和发送认证头。
我使用的Base64这里描述我如何获得基本身份验证在angularjs工作?

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

XMLHtt prequest无法加载的https://.../Category.json回调= ?无访问控制允许来源标头的请求的资源present。产地:因此 HTTP //....com '是不允许访问。响应有HTTP状态code 400。

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.

推荐答案

看起来你正在运行与跨域资源共享(CORS)的问题。有一个读:
<一href=\"http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work\">How不访问控制允许来源头工作?

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?

如果你控制了服务器,那么你可以把它发回一个适当的访问控制允许来源头按什么服务器code中的问题你引用(我得到基本身份验证在angularjs工作怎么办?

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?)

这篇关于angularjs基本认证头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆