Django rest and angularjs CORS error

I'm using django rest and angularjs 1.x in a project, in the case of this project backend and frontend are isolated, I am using a server with gulp to run angularjs, but when I try to access the REST api through the $http.get method of angular, the browser returns 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'm 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, unfortunately I did not succeed with the same, I looked for some solution both here on stack and on google in general but I did not have a favorable return.

Has anyone experienced this kind of problem? if so how to solve this impasse?

I'm grateful.

Author: dhelbegor, 2016-07-23

2 answers

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

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

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

Related:

What is the meaning of CORS?

 1
Author: Wallace Maxters, 2017-04-13 12:59:38

Install CORS in django:

pip install django-cors-headers  

Then no settings.py put in your apps:

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

And finally still in the settings.py :

CORS_ORIGIN_ALLOW_ALL = True
 0
Author: Guilherme Henrique, 2017-08-31 19:30:41