0

I am trying to make an AJAX call in my view using registerJS. My code is the following:

<?php $this->registerJs("
        $(document).on('click','.btn-block',function(){
        var id = $(this).parents('.user-tr').attr('id');
        $.ajax({
            url: "Yii::$app->request->baseUrl . 'admin/block-users'",
            type: 'POST',
            dataType: 'json',
            data: {id : id,_csrf : "Yii::$app->request->getCsrfToken()"},
            success: function (data) {
                console.log(data);
            },
        });
})"); ?>

I want the URL to be the actionBlockUsers method of AdminController, but I am getting an error:

syntax error, unexpected 'Yii' (T_STRING), expecting ',' or ')'

The error is on this line:

url: "Yii::$app->request->baseUrl . 'admin/block-users'",

This is how my back-end function looks like:

public function actionBlockUsers()
    {
        if (Yii::$app->request->isAjax) {
            //$data = Yii::$app->request->post();
            print('success');
        }
    }

How can I fix this?

UPDATE

I changed the code to this as @Bizley suggested:

$this->registerJs("
    $(document).on('click','.btn-block',function(){
    var id = $(this).parents('.user-tr').attr('id');
    $.ajax({
        url: '" . Yii::$app->request->baseUrl . "admin/block-users',
        type: 'POST',
        dataType: 'json',
        data: {id : id,_csrf : " . Yii::$app->request->getCsrfToken() . "},
        success: function (data) {
            console.log(data);
        },
    });
})"); 

The problem now is that I still can't send a request to my back end. How can I send the request?

Grigory Volkov
  • 472
  • 6
  • 26
  • 1
    Add dot `.` before `Yii` in both cases and after `getCsrfToken()`. – Bizley Aug 15 '19 at 06:25
  • Got an error `syntax error, unexpected '", ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')'` On the same line – Grigory Volkov Aug 15 '19 at 06:27
  • Okay, Iv'e added a dot after the `URL` too and the errors went away. But now I can't send the request. The inspect XHR doesn't show it either. Is this from the controller method by any chance? – Grigory Volkov Aug 15 '19 at 06:32
  • As your overall string is in `"`, you would (at least) need to escape the same type of quotes inside the string - `url: \"Yii::$app->req` (for example) – Nigel Ren Aug 15 '19 at 06:38
  • I have changed my code to the one @Bizley suggested but I still can't send the request. I will add my Controller function code in the question. Please take a look if you can. – Grigory Volkov Aug 15 '19 at 06:41

1 Answers1

1

It should be

$this->registerJs("
    $(document).on('click','.btn-block',function(){
    var id = $(this).parents('.user-tr').attr('id');
    $.ajax({
        url: '" . Yii::$app->request->baseUrl . "admin/block-users',
        type: 'POST',
        dataType: 'json',
        data: {id : id,_csrf : " . Yii::$app->request->getCsrfToken() . "},
        success: function (data) {
            console.log(data);
        },
    });
})"); 

Here are some docs you should read:

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • Thank you. I changed my code to the one in your answer. The problem is that it still doesn't send a request. I have the controller method's code added in the question. Please take a look if you can. – Grigory Volkov Aug 15 '19 at 06:44
  • It's different issue and It's hard to say what is the problem here. Please open another question with fixed JS code, explanation of the error and controller code. – Bizley Aug 15 '19 at 06:49
  • Okay, thanks for the advice. I will accept your answer since it solved my JS code problem)) – Grigory Volkov Aug 15 '19 at 06:50
  • the reason behind the solution not working is that the quotes are missing around the `Yii::$app->request->getCsrfToken()` while it is assigned to the json property as the encoded string contains `==` normally which breaks the script in unusual way that might or might not throw an error on console, you need to change the `_csrf : " . Yii::$app->request->getCsrfToken() . "` to `_csrf : '" . Yii::$app->request->getCsrfToken() . "'` – Muhammad Omer Aslam Aug 18 '19 at 07:59
  • or you can use `yii.getCsrfToken()` alternatively when working with javascript – Muhammad Omer Aslam Aug 18 '19 at 08:05