X
X
x_8ea282c02015-11-04 13:23:27
JavaScript
x_8ea282c0, 2015-11-04 13:23:27

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("Что-то пошло не так. Попробуйте снова!");
            }
        });


controller:
public function actionGetListSliderFiles()
    {
        $queryTerm = Yii::app()->request->get('ajax');
        Yii::$app->response->format = 'json';
        return ['ajax' => $queryTerm];
    }


In the controller, I just tried echo, tried if (Yii::app()->request->isAjax) { echo for example }, but nothing... F12 says error 404... no matter how hard I try... instead of ajax I tried post, changed the data types passed to the controller...
I realized that YII2 itself does not want js to knock on the controller, I wrote:
public function behaviors()
    {
        return [
            'access' => [
...
                    [
                        'actions' => ['logout', 'index', 'getlistsliderfiles'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
...
        ];
    }


But no... tell me what I'm doing wrong... everything works in basic... but Advanced resists(

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin T, 2015-11-04
@x_8ea282c0

Check again the behaviors field, most likely there is a restriction on the request type post, get.
Show your routes

H
holfza, 2015-11-04
@holfza

And if you try such an url?
/index.php?r=site/getlistsliderfiles

A
Andrew, 2015-11-04
@R0dger

do you send CSRF? if not, try turning it off, or better yet, just pass it on!

D
Dmitry Petrik, 2015-11-05
@Flexo

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 question

Ask a Question

731 491 924 answers to any question