Django Rest and Angularjs Cors error

Asked

Viewed 186 times

2

I am using Django Rest and Angularjs 1.x in a project, in case this backend project and frontend are isolated, I am using a server with Gulp to run the Angularjs, but when I try to access the Rest api through the method $http.get of angular, the browser returns me the following error:

XMLHttpRequest cannot load 0.0.0.0:8000/api/clients/?format=json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

yes, I am using Chrome, I thought the error was in the browser, so I left for Firefox, but continued the same way, I also tried to make use of Django-Cors only that, unfortunately I did not succeed with it, I looked for some solution both here in stack and google in general but I did not have a favorable return.

Has anyone ever been through this kind of problem? if yes how to solve this problem?

in advance I am grateful.

2 answers

1


The problem is that requests must be from the same origin when you use a request XmlHttpRequest.

You need to configure the application that provides the data to accept the source from where you are trying to capture the request (the application made in angular).

You can use the library Django Cors Headers so that the requests no longer have this error.

Related:

What is the meaning of CORS?

0

Install Cors on Django:

pip install django-cors-headers  

Then in Settings.py put in your apps:

INSTALLED_APPS = [
..............
'corsheaders',
]

And last still in Settings.py :

CORS_ORIGIN_ALLOW_ALL = True

Browser other questions tagged

You are not signed in. Login or sign up in order to post.