Answer the question
In order to leave comments, you need to log in
Ajax + YII2 + Advanced = Error 404. Why?
Good afternoon everyone!
I recently moved to YII2, a very powerful tool... but there are problems that I don't fully understand...
The meaning is simple, I press the button, Ajax knocks on the controller and gives the result. Implementation:
Ajax
$.ajax({
url: "?r=site/getlistsliderfiles",
data: {ajax: SourceInSlider},
type: "POST",
//dataType : "html",
success: function(data) {
alert(data);
},
error: function( xhr, status, errorThrown ) {
alert("Что-то пошло не так. Попробуйте снова!");
}
});
public function actionGetListSliderFiles()
{
$queryTerm = Yii::app()->request->get('ajax');
Yii::$app->response->format = 'json';
return ['ajax' => $queryTerm];
}
public function behaviors()
{
return [
'access' => [
...
[
'actions' => ['logout', 'index', 'getlistsliderfiles'],
'allow' => true,
'roles' => ['@'],
],
...
];
}
Answer the question
In order to leave comments, you need to log in
Check again the behaviors field, most likely there is a restriction on the request type post, get.
Show your routes
do you send CSRF? if not, try turning it off, or better yet, just pass it on!
There are two points
here 1. Incorrect url in the Ajax request. You have written "?r=site/getlistsliderfiles", you need "site/getlistsliderfiles". It is better to generate a link to the action via the Url::to() helper in the future, this will allow you to avoid a lot of problems.
2. This name of the action is unacceptable. You cannot use different case. Correct would be: actionGetlistsliderfiles()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question