Leo*_*rdo 5 django rest angularjs django-rest-framework django-allauth
我正在创建一个Web应用程序,使用Django作为后端,Angular作为前端.
localhost:9000当Django正在运行时localhost:8000,Angular正在Yeoman堆栈上运行,我正在使用grunt-contrib-proxy将所有$http调用从angular at 重定向/api到django端口.因此,例如,如果Angular要求localhost:9000/api/hello将重定向到,localhost:8000/api/hello并且django将为其提供服务.
我打算设置Django Rest Framework为/api路径提供所有Angular请求.
到现在为止还挺好.
现在,我已经配置了一个Django-allauth用于Oauth对第三方服务进行身份验证的工作安装.它使用普通的旧Django工作,但我不知道如何与Angular一起使用它.
唯一想到的是allauth通过django rest框架提供视图,但是在身份验证后重定向呢?我无法绕过它.
是不是更好地放弃这种方法并直接从前面(Angular)进行Oauth身份验证?
编辑:
我设法login从Angular 调用视图
在grunt-contrib-proxy中我添加了帐户上下文和重写规则:
context: ['/api', '/accounts'],
rewrite: {
'^/api': '/api',
'^/account': '/accounts'
}
Run Code Online (Sandbox Code Playgroud)我已经从angular进行了ajax调用,要求allaluth登录视图(例如对于github): $http.get('/accounts/github/login/?process=login')
问题是我回来了:
XMLHttpRequest cannot load https://github.com/login/oauth/authorize?scope=&state=BlaBla&redirect…ub%2Flogin%2Fcallback%2F&response_type=code&client_id=BlaBlaBla. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. (index):1
Run Code Online (Sandbox Code Playgroud)
(BlaBla是我添加的).我想我做错了
您需要添加一个
Origin: http://localhost:9000
Run Code Online (Sandbox Code Playgroud)
到 Angular 发送到 Django 的请求中的标头。
还要确保运行 Django 的服务器返回
Access-Control-Allow-Origin: *
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1302 次 |
| 最近记录: |