简体   繁体   中英

Unable to post a cross origin request in Django website

Even after installing CORS middle ware and enabling Cross Origin Requests also. I am unable to make a post request by using Javascript.

const csrf = Cookies.get('csrftoken');
    return {
    'X-CSRFToken' : csrf,
    'Access-Control-Allow-Origin':"*"
    "Access-Control-Allow-Headers":"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"};

And also in Django app

CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'127.0.0.1',
)

CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken',
'x-api-key'
)

And also after adding CORS Middleware,

MIDDLEWARE_CLASSES = ('corsheaders.middleware.CorsMiddleware')

Maybe you need to set Access-Control-Request-Methods. Something like this

"Access-Control-Request-Methods": "OPTIONS, HEAD, GET, POST"

Check this question Default value for Access-Control-Request-Methods

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