简体   繁体   中英

django-cors-headers CORS_ORIGIN_WHITELIST tuple vs string issue

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" );
});

使用CORS_ORIGIN_WHITELIST作为元组

使用CORS_ORIGIN_WHITELIST作为字符串

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM