Ajax Post 400 Bad Request

Hello everyone,

I’m new to the Yii Framework and I try to make an Ajax Request.

I need to do an autosave (drafts) functionnality for a messaging system.

But there is a problem, when I do a GET ajax request, it works perfectly

but when I try to make a POST request it says : "POST 400 (Bad Request)"

I tried a simple call with a simple callback like this

compose view (compose.php) :


$.ajax(

{

	url: 'autosave',

	type: 'POST',

	data: {"test":"hello"},

	dataType: 'json',

	contentType: "application/json; charset=utf-8",

	success: function(id)

	{

		console.log('ID : ' + id);

	}

});

MessageController.php :


public function actionAutosave() {

		

	echo $_POST['test'];

}

Thank you for the help :)

I found the problem on the Reponse Header it was causing by the YII_CSRF_TOKEN that was not submitted on the post ajax request so I added it :)

/* moved to General Discussion */

(not a tip, snippet or tutorial)