简体   繁体   中英

How to set a X-CSRF token in $resource

I'd like to add an X-CSRF token to some $resources, but even though I've been looking to other topic and found taht I could add header this way

return $resource(url, {}, {
              connect: {
                method: 'POST',
                headers: {'headerName': value}
              }
            });

However I don't have the X-CSRFToken added in the request headers when I set headerName to CSRF-TOKEN or crsftoken, (same goes for X-CSRF-TOKEN )

Do someone has an idea why it's not working ?

Edit1: I've seen that add a cookie named XSRF-TOKEN added an header X-XSRF-TOKEN to my requests but CSRF-TOKEN does not.

Here is an example

this.exampleFunction = function (csrf) {
    return $resource(url, null, {
        'update': {'method': 'POST', headers: {'HTTP_X_CSRFTOKEN': csrf}},
    });
};

The header name may be different depending on the back end. For Django it may be 'X-CSRFToken' or 'HTTP_X_CSRFTOKEN'.

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