1

I'm trying to use django-cors-headers for my project.

It appears when I set CORS_ORIGIN_WHITELIST as a string it works fine. But when I use it as a tuple it doesn't work. Any idea why? I can't find anything specific in the documentation about the difference between using a tuple or string.

To load the JSON I'm using jQuery $.getJSON()

$.getJSON( "http://127.0.0.1:8000/accounts/api_r/44234138/?format=json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
  });

  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "#foo" );
});

Using CORS_ORIGIN_WHITELIST as a tuple

Using CORS_ORIGIN_WHITELIST as a string

1 Answers1

2

I was having this same problem. I believe the issue has to do with string encoding. If you change your whitelist to the following it should work for you:

CORS_ORIGIN_WHITELIST = (
    u'http://localhost:8888',
    u'http://127.0.0.1:8000',
)

Unfortunately I don't have a "why" for you, but at least this should get you going.

Melanie
  • 56
  • 4
  • basic rule is you should cover all the base urls . like I was using react and as react app was at port 3000 i had to include it in the whitelist. my whitelist was something like this - CORS_ORIGIN_WHITELIST = [ 'http://localhost:3000', 'http://localhost:8000', 'http://localhost:8080', ] – Almuntasir Abir Jul 27 '20 at 08:30
  • and don't forget to add the cors middleware like this: 'corsheaders.middleware.CorsMiddleware' – Almuntasir Abir Jul 27 '20 at 08:35