M
M
makarov19912019-11-23 11:38:36
Yii
makarov1991, 2019-11-23 11:38:36

How to get rid of 400 bad request error in Yii2?

Good afternoon. The error occurs during ajax request.
I need to pair vue + yii2 (where vue works as a library)
here is the code

new Vue({
    el: '#app',
    data(){
        return {}
    },
    methods : {
        send(){
            const data = {
                data :{ },
                _csrf : yii.getCsrfToken()
            }
        
            this.\$http.post('/client/analytics/test',data).then(response => {
                console.info(response);
            })
        }
    }
});

I get an error
5dd8ef7adb9ca227793601.jpeg
At the same time, for the "test" I do the same through Jq and everything works
$.ajax({
            url: '/client/analytics/test',
            type: 'post',
            data: {
                data: 'Hello',
                _csrf : yii.getCsrfToken()
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data);
            }
        });

Note: I don't want to disable server side validation using $this->enableCsrfValidation = false;
What do you think is the reason for 400 bad request in Yii2 in my (vue) case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EvgMul, 2019-11-28
@EvgMul

Try specifying In the controller or in a specific action method.
beforeAction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question