This is a pretty simple test before I implement some larger functionality. Eventually this will be just a glorified upload form via ajax.
Part 1 - Ajax post
$.ajax({
type: 'POST',
url: '/upload2',
dataType: "json",
data: JSON.stringify({"abc": "123", csrfTokenName: csrfTokenValue}),
success: function(data, textStatus){
alert('form submitted successfully');
alert('data'+data);
},
error: function (data, ErrorText, thrownError) {
console.log("Failed to process request correctly, please try again");
console.log(data);
}
});
Part 2 - Plugin
Plugin is installed. Inside my plugin init I've got a site rule setup:
$event->rules['upload2'] = 'plugin/default/upload';
I can visit this url in the browser without issue. Here's my function I'm trying to access:
public function actionUpload()
{
$response_array['status'] = 'success';
$response_array['post'] = $_POST;
return json_encode($response_array);
}
When I try and run said code via ajax, console returns Bad Request: Unable to verify your data submission. and a 400 (Bad Request) error to boot.
This could easily be one of those doh! moments but so far I'm losing this war. Should I just quit the internet and go live in the woods? (kidding)
Anyone have any idea what I'm doing wrong here? Are ajax posts different in Craft 3?