我有一个使用angular 6作为前端框架和Django后端的应用程序。当我使用get request时一切正常,但是每当我要使用post时,我都会遇到403 forbidden
错误。试图在Django上使用headers={'Access-Control-Allow-Origin': '*'
,但效果不佳。
这是我的代码:
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())
}
在check-edit.component.html上调用 editCheck:<button mat-button (click)="editCheck()" >Save</button>
查看:
class GetChecksView(APIView):
renderer_classes = [renderers.JSONRenderer]
def get(self, request):
checks = PingCheck.objects.all()
checks = PingCheckSerializer(checks, many=True).data
return Response(checks)
这是错误的样子:
polyfills.js:1 POST http://127.0.0.1:8000/api/checks 403(禁止访问)
main.js:1错误e {header:t,status:403,statusText:“ Forbidden”,
url:“ http://127.0.0.1:8000/api/checks”,确定:false,...}