<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>

CC BY-SA 4.0
2018-07-19T15:46:41.390Z
2018-07-19T15:46:41.390Z
Robin Schambach
<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>

<javascript>
Make an Ajax call to plug-in controller
# 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:

  {{ csrfInput() }}
  
  Test


and this is my js code:



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


---

