3

I have an application that uses angular 6 as front framework & Django on backend. When I use get request everything is ok, but whenever I want to use post I face 403 forbidden error. Tried to use headers={'Access-Control-Allow-Origin': '*' on Django but it didn't work any good. Here's my code:

Check.service.ts:

editCheck(check: Check):Observable<any> {
  return this.http.post(this.checksUrl, check,httpOptions)
}

check-edit.component.ts:

   goBack():void{
this.location.back();
}
editCheck(): void {
this.checksService.editCheck(this.check)
  .subscribe(() =>this.goBack())
}

editCheck is called at check-edit.component.html: <button mat-button (click)="editCheck()" >Save</button>

View:

class GetChecksView(APIView): 
    renderer_classes = [renderers.JSONRenderer] 
    def get(self, request): 
        checks = PingCheck.objects.all() 
        checks = PingCheckSerializer(checks, many=True).data 
        return Response(checks)

And here is what the error looks like:

polyfills.js:1 POST http://127.0.0.1:8000/api/checks 403 (Forbidden)
main.js:1 ERROR e {headers: t, status: 403, statusText: "Forbidden",
url: "http://127.0.0.1:8000/api/checks", ok: false, …}

Selcuk
  • 57,004
  • 12
  • 102
  • 110

0 Answers0