$.ajax Post Return Error 400 Bad Request

Hi,

I have this controller function generated by gii


	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			

			// we only allow deletion via POST request

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}



I need to call this function in javascript and my js code is


$.ajax

({

	url: "/people/delete",

	type: "post",

	data: { YII_CSRF_TOKEN: $("#YiiCsrf").val(), id: $(this).attr("name") },

	success: function(response, textStatus, jqXHR)

	{

		alert(response);

	},

	error: function(jqXHR, textStatus, errorThrown)

	{

		alert(jqXHR.status + ' ' + textStatus + ' - ' + errorThrown);

	},

	complete: function()

	{

	}

});



I got error 400 Bad Request. Anything wrong?

I may be completely wrong, but I believe only GET data is passed into the action’s parameters by default. This thread may point you in the right direction:

http://www.yiiframework.com/forum/index.php/topic/15306-post-support-for-action-parameter-binding/

Keith is completely right :)

I have changed to use type: "get" and modify the actionDelete to accept get request.

Thanks all.

That’s a bad practice.

Delete should be done via POST (or DELETE).

Just change this line


url: "/people/delete",

to


url: "/people/delete/id/" + $(this).attr("name"),