Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - getting strict-origin-when-cross-origin although i provided CORS_ORIGIN_ALLOW_ALL = True in settings.py file

I have build my frontend using Angular and backend using django-restframwork. In my settings.py I provided CORS_ORIGIN_ALLOW_ALL = True I run it using python3.8 manage.py runserver 5000 but when my frontend tried to send request to it it generates error.

my angular sends request like this.

my environment file where I saved my urls

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/"

};

and by submitting register form I send my request like this.

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

these are in my http service where I call using the above code.

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,
      });
    }
    
  }

can anyone tell me why I am getting cross-origin error and what is the problem.

I tried:

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 is my domain name

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/"

}; 

none of the above worked for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to run this command: python3.8 manage.py runserver 0.0.0.0:5000

Detail here: https://docs.djangoproject.com/en/3.1/ref/django-admin/#runserver


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...