<p>For Craft 2, these are the values you're looking for...</p>

<pre class="lang-js prettyprint-override"><code>window.csrfTokenName = "{{ craft.config.get('csrfTokenName') }}";
window.csrfTokenValue = "{{ craft.request.getCsrfToken }}";
</code></pre>

<p>They changed slightly in Craft 3, which can be seen <a href="https://craftcms.stackexchange.com/a/6588/45">here...</a></p>

CC BY-SA 4.0
2019-03-04T16:47:55.180Z
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;
WTFranklin
CC BY-SA 4.0
2019-03-04T22:01:19.240Z
Hmm, not sure why you'd still be getting a 400. I'd recommend popping over to [Craft Discord](https://craftcms.com/discord), where it'll be easier to discuss the issue in greater detail.
Lindsey D
CC BY-SA 4.0
2019-03-02T00:45:03.370Z
2019-03-02T00:45:03.370Z
Lindsey D
<p>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:</p>

<pre><code>$config['*'] = [
...
    'enableCsrfProtection' =&gt; true,
...
],
</code></pre>

<p>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:</p>

<pre><code>&lt;script&gt;
        window.csrfTokenName = "{{ craft.config.csrfTokenName }}";
        window.csrfTokenValue = "{{ craft.request.csrfTokenValue }}";
&lt;/script&gt;
</code></pre>

<p>and the <code>window.csrfTokenName = "CRAFT_CSRF_TOKEN";</code> but <code>window.csrfTokenValue = "";</code> </p>

<p>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!</p>

<ajax><craft2><csrf>
CSRF token is null or empty in craft 2
# CSRF token is null or empty in craft 2

(Asked by: username_0 on 2019-03-02)

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:


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!

**Tags:** ajax, craft2, csrf


---

## 1 answers

### Answer by username_1 on 2019-03-02. Score: 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...

> **Comments:**
> **username_0**: 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;  
> **username_1**: Hmm, not sure why you'd still be getting a 400. I'd recommend popping over to [Craft Discord](https://craftcms.com/discord), where it'll be easier to discuss the issue in greater detail.  

---

