Answers.Body: <p>You need to define a route via action param otherwise Craft has no idea where to route the request. Your <code>data</code> variable only only contains</p>
  
  <pre><code>data = [
      'test' =&gt; 'test',
      TOKEN_NAME =&gt; TOKEN_VALUE
  ]
  </code></pre>
  
  <p>and nothing else</p>
  
  <p>You need to include <code>action =&gt; plugin-handle/controller-id/function-name</code></p>
  
  <p>You can see a working example at <a href="https://craftcms.stackexchange.com/questions/24166/basic-ajax-how-to/24167#24167">basic ajax how to</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-19T15:46:41.390Z
Answers.LastActivityDate: 2018-07-19T15:46:41.390Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am trying to send data to my plug-in controller using jquery $.post() method.
  here is my form:</p>
  
  <pre><code>&lt;form method="post" action="" accept-charset="UTF-8" id="testForm"&gt;
    {{ csrfInput() }}
    &lt;input type="hidden" name="action" value="test/test"&gt;
    &lt;button type="submit"&gt;Test&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and this is my js code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
     window.csrfTokenName = "{{ craft.config.csrfTokenName|e('js') }}";
     window.csrfTokenValue = "{{ craft.request.csrfToken|e('js') }}";
  &lt;/script&gt;
  &lt;script&gt;
  $("#testForm").submit(function (e) {
      e.preventDefault();
      var data = [];
      data["test"] = "test";
      data[csrfTokenName] = csrfTokenValue;
      $.post('/', data, function(response) {
          console.log(response);
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>The console keeps logging "400 Bad Request".
  What am I doing wrong ?</p>
  
Tags: <javascript>
Title: Make an Ajax call to plug-in controller
ThreadText: # Make an Ajax call to plug-in controller
  
  (Asked by: username_0 on 2018-07-19)
  
  I am trying to send data to my plug-in controller using jquery $.post() method.
  here is my form:
  
  ```
  <form method="post" action="" accept-charset="UTF-8" id="testForm">
    {{ csrfInput() }}
    <input type="hidden" name="action" value="test/test">
    <button type="submit">Test</button>
  </form>
  
  ```
  
  and this is my js code:
  
  ```
  <script type="text/javascript">
     window.csrfTokenName = "{{ craft.config.csrfTokenName|e('js') }}";
     window.csrfTokenValue = "{{ craft.request.csrfToken|e('js') }}";
  </script>
  <script>
  $("#testForm").submit(function (e) {
      e.preventDefault();
      var data = [];
      data["test"] = "test";
      data[csrfTokenName] = csrfTokenValue;
      $.post('/', data, function(response) {
          console.log(response);
      });
  });
  </script>
  
  ```
  
  The console keeps logging "400 Bad Request".
  What am I doing wrong ?
  
  **Tags:** javascript
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-07-19. Score: 1
  
  You need to define a route via action param otherwise Craft has no idea where to route the request. Your `data` variable only only contains
  
  ```
  data = [
      'test' => 'test',
      TOKEN_NAME => TOKEN_VALUE
  ]
  
  ```
  
  and nothing else
  
  You need to include `action => plugin-handle/controller-id/function-name`
  
  You can see a working example at [basic ajax how to](https://craftcms.stackexchange.com/questions/24166/basic-ajax-how-to/24167#24167)
  
  
  ---
  
  