1

I'm having an issue with Craft 2.6 and the CSRF token. I enabled Csrf Protection in my general.php file by adding this line $config['*'] array so it looks like:

$config['*'] = [
...
    'enableCsrfProtection' => true,
...
],

I also tried adding this line to the section for // Global variables after this array, but still no luck. I try to attach the token to the window object like this:

<script>
        window.csrfTokenName = "{{ craft.config.csrfTokenName }}";
        window.csrfTokenValue = "{{ craft.request.csrfTokenValue }}";
</script>

and the window.csrfTokenName = "CRAFT_CSRF_TOKEN"; but window.csrfTokenValue = "";

I can't figure out how else to get a csrf token and I can't find any documentation that says anything else. I'm just trying to see if I'm missing something really easy or if there is actually something else I need to add. Any help is greatly appreciated!

2

For Craft 2, these are the values you're looking for...

window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}";
window.csrfTokenValue = "{{ craft.request.getCsrfToken }}";

They changed slightly in Craft 3, which can be seen here...

|improve this answer|||||
  • Hey thank you so much, I was able to get the token name and value now. I'm still getting the 400 error even after adding this to the array I'm passing in the ajax request. I'm adding it like this: data[window.csrfTokenName] = window.csrfTokenValue; – Frank A. Mar 4 '19 at 16:47
  • Hmm, not sure why you'd still be getting a 400. I'd recommend popping over to Craft Discord, where it'll be easier to discuss the issue in greater detail. – Lindsey D Mar 4 '19 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.