Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-28T17:09:27.773Z
Answers.LastActivityDate: 2018-08-28T17:09:27.773Z
Answers.UserDisplayName: bstnhnsl
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-20T21:23:30.617Z
Comments.Text: 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.
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-21T08:18:33.340Z
Comments.Text: That helped. had a misspelling in my ajax template name (started with a blank space... awesome.)
Comments.UserDisplayName: bstnhnsl
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-23T04:07:41.633Z
Comments.Text: @bstnhnsl Would you mind adding your solution as an official answer in case it ends up helping someone in the future?
Comments.UserDisplayName: Brad Bell
Tags: <ajax><csrf>
Title: CSRF Protection in AJAX post
ThreadText: # 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:
  
  ```
  <script type="text/javascript">
      window.csrfTokenName = "{{ craft.app.config.general.csrfTokenName }}";
      window.csrfTokenValue = "{{ craft.app.request.csrfToken }}";
  </script>
  
  ```
  
  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**: @username_0 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 username_1 username_1 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.
  
  
  ---
  
  