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.