<p>As Robin Schambach suggested I looked through my stack trace.
The request was fine but php couldn't find the template used on ajax-requests due to a misspelling error.</p>

<p>Fixed the misspelling – works like a charm.</p>

CC BY-SA 4.0
2018-08-28T17:09:27.773Z
2018-08-28T17:09:27.773Z
bstnhnsl
<p>I'm having some trouble passing CSRF-tokens in my js-File in Craft 3.</p>

<p>I'm setting the values in my template like so:</p>

<pre><code>&lt;script type="text/javascript"&gt;
    window.csrfTokenName = "{{ craft.app.config.general.csrfTokenName }}";
    window.csrfTokenValue = "{{ craft.app.request.csrfToken }}";
&lt;/script&gt;
</code></pre>

<p>and call them in my .js-file:</p>

<pre><code>var data = {id: 100 };
    data[window.csrfTokenName] = window.csrfTokenValue;
    $.ajax({
        type: 'POST',
        url: href,
        data: data,
        success: function(result){
        },
        error: function(){
        }
    });
</code></pre>

<p>before i set the csrf tokens, the server returned an error 400 – bad request – as you would expect.
now i'm getting a 500 internal server error.
What am I doing wrong?</p>

<p>The site works perfect without the ajax-call so I don't think there's an issue with my templates. Tell me if you need to see more of my code.</p>

CC BY-SA 4.0
2018-08-20T21:23:30.617Z
You might want to take a look at your stack trace because a 500 states in fact the request is fine from technical frontend point of view but there is a PHP exception.
Robin Schambach
CC BY-SA 4.0
2018-08-21T08:18:33.340Z
That helped. had a misspelling in my ajax template name (started with a blank space... awesome.)
bstnhnsl
CC BY-SA 4.0
2018-08-23T04:07:41.633Z
@bstnhnsl Would you mind adding your solution as an official answer in case it ends up helping someone in the future?
Brad Bell
<ajax><csrf>
CSRF Protection in AJAX post
# CSRF Protection in AJAX post

(Asked by: username_0 on 2018-08-20)

I'm having some trouble passing CSRF-tokens in my js-File in Craft 3.
I'm setting the values in my template like so:


and call them in my .js-file:
var data = {id: 100 };
    data[window.csrfTokenName] = window.csrfTokenValue;
    $.ajax({
        type: 'POST',
        url: href,
        data: data,
        success: function(result){
        },
        error: function(){
        }
    });

before i set the csrf tokens, the server returned an error 400 – bad request – as you would expect.
now i'm getting a 500 internal server error.
What am I doing wrong?
The site works perfect without the ajax-call so I don't think there's an issue with my templates. Tell me if you need to see more of my code.

**Tags:** ajax, csrf
> **Comments:**
> **username_1**: You might want to take a look at your stack trace because a 500 states in fact the request is fine from technical frontend point of view but there is a PHP exception.  
> **username_0**: That helped. had a misspelling in my ajax template name (started with a blank space... awesome.)  
> **username_2**: @bstnhnsl Would you mind adding your solution as an official answer in case it ends up helping someone in the future?  

---

## 1 answers

### Answer by username_0 on 2018-08-28. Score: 0 | Accepted answer

As Robin Schambach suggested I looked through my stack trace.
The request was fine but php couldn't find the template used on ajax-requests due to a misspelling error.

Fixed the misspelling – works like a charm.


---

