G
G
good_beginer2019-01-14 18:00:42
Yii
good_beginer, 2019-01-14 18:00:42

Why is ajax request not working in yii2?

Are there any special request criteria for yii2? no matter how I twist everything 400, it throws an error (bad request)

$(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "<?php echo Yii::$app->getUrlManager()->createUrl('/partner/ajax')  ; ?>",
            data: {test: "test"},
            success: function (data) {
                alert(JSON.stringify(data));
            },
            error: function (exception) {
                alert(JSON.stringify(exception));
            }
        });
    });

public function actionAjax()
    {
                if(!empty(Yii::$app->request->post('test'))){
            $test = "Ajax Worked!";
        }else{
            $test = "Ajax failed";
        }
return $test;
    }

request couldn't be easier

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2019-01-14
@good_beginer

Disable csrf token verification. Well, or pass it along with the request data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question