Yii Framework Forum: Ajax Post 400 Bad Request - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Post 400 Bad Request Ajax post don't work but GET Rate Topic: ***** 1 Votes

#1 User is offline   Romain7 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-February 13

Posted 04 February 2013 - 05:04 AM

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 :)
0

#2 User is offline   Romain7 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-February 13

Posted 04 February 2013 - 06:54 AM

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 :)
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 08 February 2013 - 05:11 PM

/* moved to General Discussion */
(not a tip, snippet or tutorial)
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users