CORS Error with Angular 13 Frontend and Django Backend [duplicate]

I’m currently working on a project using Angular 13 for the frontend and Django for the backend. I’ve developed a login page that fetches a background image via a GET API call from the Django backend. However, I’m encountering a CORS (Cross-Origin Resource Sharing) error when making the request.

Here’s a summary of the issue:

  • Frontend: Angular 13 application
  • Backend: Django application
  • Feature: Login page fetching background image through a GET request

Issue:

I’ve attempted to resolve the CORS error by adding the Access-Control-Allow-Origin header in the backend response, but the error persists. I’m unsure whether this issue should be resolved on the frontend side or the backend side.

Steps Taken:

  • Added ‘Access-Control-Allow-Origin’ header in request header.
  • installed cors extension in browser.

Code and Images:

I have attached relevant code snippets and images for better understanding.

login.service.ts


[![enter image description here][1]][1]
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import { environment } from '../environments/environment';
@Injectable({
  providedIn: 'root'
})
export class LoginService {
  rootURL:any;
  constructor(public http:HttpClient) {
    this.rootURL = environment.apiUrl;
  }
  getBackGroundImage(){
    let header = {
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*'
    }
    return this.http.get(`${this.rootURL}/login/cors/img/backgrounds/login/28.jpg?1907c5373b7`,{headers: header});
  }
}

login.component.ts

import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {LoginService} from '../services/login.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  loginForm!: FormGroup;
  hasError = false;
  errorMessage = '';
  constructor(public loginService:LoginService) {
    this.loginService.getBackGroundImage().subscribe((response: any) => {
      console.log('response', response);
    },(error:any)=>{
      console.error(error.message);
    })
  }

  ngOnInit(): void {
    this.loginForm = new FormGroup({
      emailFormControl : new FormControl('', [Validators.required, Validators.email]),
      passwordFormControl : new FormControl('', [Validators.required])
    })
  }
  logIn(): any{
    debugger;
      if(this.loginForm.value.emailFormControl == "" || this.loginForm.value.passwordFormControl == ""){
        this.hasError = true;
        this.errorMessage = 'Please, type your login and password in form above';
      }
      else if(this.loginForm.value.emailFormControl !== 'mohit.k.sharma2' && this.loginForm.value.passwordFormControl !== "test"){
        this.hasError = true;
        this.errorMessage = 'Login or password is incorrect';
      }
      else if(this.loginForm.value.emailFormControl == 'mohit.k.sharma2' && this.loginForm.value.passwordFormControl.value == "test"){
        this.errorMessage = '';
        this.hasError = false;
      }
  }
}

Questions:

  1. Is there a specific configuration needed in Django to handle CORS correctly?
  2. Are there additional settings or headers that need to be configured on the frontend or backend to resolve this issue?
  3. Can you provide guidance on debugging and fixing CORS errors in this context?

Thank you for your assistance!

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật