尽管我在 settings.py 文件中提供了 CORS_ORIGIN_ALLOW_ALL = True

问题描述

我使用 Angular 构建了我的前端,使用 django-restframwork 构建了后端。在我的 settings.py 中,我提供了 CORS_ORIGIN_ALLOW_ALL = True 我使用 python3.8 manage.py runserver 5000 运行它但是当我的前端尝试向它发送请求时它会产生错误

我的角度发送这样的请求。

我保存网址的环境文件

export const environment = {
  production: true,api_url: "https://localhost:5000/op-api",user_login_url: "https://localhost:5000/auth/login/",// all_users :"https://ots.ahg.af:5000/rest-auth/login",user_logout_url: "https://localhost:5000/auth/logout/",user_register_user:"https://localhost:5000/auth/register/"

};

通过提交注册表,我会像这样发送我的请求。

 onCreateAccount(){ 
    this.httpService.register_user(this.registerForm.value).subscribe(resp=>{
      .......
    },error=>{
    ......
      } else{
       ........
    });
  }

这些在我使用上述代码调用的 http 服务中。

user_register_user = environment.user_register_user;

 public register_user(data){
    if (data != null){
      return this.http.post(this.user_register_user,{
        'username': data.username,'email' : data.email,'password' : data.password,});
    }
    
  }

谁能告诉我为什么会出现跨域错误以及问题出在哪里。

我试过了:

export const environment = {
  production: true,api_url: "https://ots.ahg.af:5000/op-api",user_login_url: "https://ots.ahg.af:5000/auth/login/",user_logout_url: "https://ots.ahg.af:5000/auth/logout/",user_register_user:"https://ots.ahg.af:5000/auth/register/"

};

ots.ahg.af 是我的域名

export const environment = {
  production: true,api_url: "http://127.0.0.1:5000/op-api",user_login_url: "http://127.0.0.1:5000/auth/login/",// all_users :"http://127.0.0.1:8000/rest-auth/login",user_logout_url: "http://127.0.0.1:5000/auth/logout/",user_register_user:"http://127.0.0.1:5000/auth/register/"

}; 

以上都不适合我。

解决方法

尝试运行此命令:python3.8 manage.py runserver 0.0.0.0:5000

此处的详细信息:https://docs.djangoproject.com/en/3.1/ref/django-admin/#runserver