0
function facebookAuth() {
    $.ajax({
        url: 'index.php?r=account/fbauthorize',
        type: 'GET'
    });
}

it is a simple function i wrote just to invoke the function in the controller, the GET type works fine, but the POST does not. give me this error "Bad Request (#400): Unable to verify your data submission."

it is something to do with CSRF validation in yii2, but i can't solve it.

1

3 Answers 3

4

There are two important steps:

1) Register your js file as follows:

$this->registerJsFile(Yii::$app->homeUrl . 'js/test.js', [JqueryAsset::className()]);

2) In ajax request you need to post following value along with data:

yii.getCsrfParam(): yii.getCsrfToken()

CSRF is a security feature which can be disabled in controller, but it is not recommended.

Sign up to request clarification or add additional context in comments.

4 Comments

hi, sorry for the late reply, i am not sure where to register my file. in what file i mean.
your have to rejister js on your view file and the path js/test.js have full path as webroot/yourproject/frontend/web/js/test.js in advance templet. tell me if you dont understand
thank you man i got it working since yesterday, you were of great help, thanks.
How do you print csrf in external js file?
0

add this two line to your code

contentType: "application/json; charset=utf-8",
dataType: "json",

this will be

$.ajax({
    url: 'index.php?r=account/fbauthorize',
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
});


Enjoy :)

2 Comments

this does not work. and pls could u elaborate what this supposed to do? also i stated that the GET type works fine, it is the POST type that i'm having trouble with!
0

Make sure to set the right access method.

public function behaviors()
{
    return [
        'verbs' => [
            'class' => \yii\filters\VerbFilter::className(),
            'actions' => [
                'fbauthorize'  => ['post'],
            ],
        ],
    ];
}

http://www.yiiframework.com/doc-2.0/yii-filters-verbfilter.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.