I have CSRF enabled on my install. I have a bunch of frontend forms so CSRF is pretty important for me. However, when I use AJAX to post to a controller action I get the following error.
<div id="message" class="pane">
<h2>Bad Request</h2>
<p>The CSRF token could not be verified.</p>
</div>
Here is my AJAX that I am testing with.
$(function(){
var data = { id: 100 };
$.post("/actions/gistClient/exampleAjax", data, function(response) {
console.log("success", response, data);
});
});
Here is my controller action
public function actionExampleAjax()
{
$this->requireAjaxRequest();
// ... whatever your AJAX does...
$response = array('response' => 'Round trip via AJAX!');
$this->returnJson($response);
}
Is it possible to disable CSRF for certain routes? Alternatively, how would I pass CSRF through AJAX to the controller action?