Django web app reaching rest api and CORS problem

90 views
Skip to first unread message

Oguz Yarimtepe

unread,
Nov 2, 2016, 9:29:25 AM11/2/16
to Django users
I have a django app and at the template part i have javascript function calling the rest api:


<!-- Custom Theme JavaScript -->
<script src="/static/js/select-autofiller.js"></script>


At the js file i am filling a select box according to the returned json response

$('#field1').change(function(){

var var1=$("#filed2").val();
var var2=$("#field1 option:selected").text();


url="http://www.foo.com:8080/" + var1 + "/" + var2 + "/";


$.get(url, function( data ) {
alert( "Load was performed." + data);
});



});

The Django web app is running on www.foo.com:8000

At the rest api side, falcon is running and i installed falcon-cors

import falcon

from wsgiref import simple_server

from falcon_cors import CORS


check_python()

cors = CORS(allow_origins_list=settings.ALLOWED_ORIGINS)

api = falcon.API(
media_type='application/json',
middleware=[
# json.RequireJSON(),
# json.JSONTranslator(),
https.RequireHTTPS(),
headers.BaseHeaders(),
handle_404.WrongURL(),
cors.middleware
],
)

***
route definitions
****

httpd = simple_server.make_server('0.0.0.0', 8080, api)
httpd.serve_forever()

ALLOWED_ORIGINS is a list: ['www.foo.com:8000', 'www.foo.com:8080']

Still when i select the box and triggered the js at the Django web app, i am getting 

XMLHttpRequest cannot load http://www.foo.com:8080/var1/var2. Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.foo.com:8000' is therefore not allowed access. The response had HTTP status code 400.

I couldn't find ow to fix it. 
Any idea?

Siphiwe Gwebu

unread,
Feb 21, 2018, 7:17:48 AM2/21/18
to Django users
Did you manage to sort this out? Got the same issue...

Andy

unread,
Feb 21, 2018, 2:14:26 PM2/21/18
to Django users
Why do you use a third component to have a rest api? instead of the django rest framework?

Since the problem is inside your custom falcon server, its not in any way related to django .. ask the falcon comunity here instead: https://gitter.im/falconry/user
Reply all
Reply to author
Forward
0 new messages