【问题标题】:getting strict-origin-when-cross-origin although i provided CORS_ORIGIN_ALLOW_ALL = True in settings.py file尽管我在 settings.py 文件中提供了 CORS_ORIGIN_ALLOW_ALL = True
【发布时间】:2021-04-21 05:47:02
【问题描述】:

我已经使用 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/",
  // all_users :"https://ots.ahg.af:5000/rest-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/"

}; 

以上都不适合我。

【问题讨论】:

  • 您是否安装了django-cors-headers 并将corsheaders 添加到INSTALLED_APPScorsheaders.middleware.CorsMiddlewareMIDDLEWARE 的顶部?
  • @xcvbn 是的,我做到了

标签: python django django-rest-framework referrer-policy


【解决方案1】:

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

详情在这里:https://docs.djangoproject.com/en/3.1/ref/django-admin/#runserver

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2014-04-27
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多