I
I
Ivan Petrov2017-06-05 10:51:30
Yii
Ivan Petrov, 2017-06-05 10:51:30

How to specify the path to the action in Yii in a JS file?

Hello. Is it possible to specify the path to the Action in the JS file at all?

$('.who').bind("change keyup input click", function() {
                if (this.value.length >= 2) {
                    $.ajax({
                        type: 'post',
                        url: "<?= $this->createUrl('city/City')?>", //Путь к обработчику
                        data: { 'referal': this.value },

                        response: 'text',
                        success: function(data) {
                            $(".search_result").html(data).fadeIn(); 
                        }
                    })
                }
            })

I tried to specify both the full path and through creatURL. If this is not possible, then where should the JS be placed in order to safely access the handler in the action? Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
matperez, 2017-06-05
@matperez

It would be more correct to either pass the url through the data attribute and get it in js through something like $(this).data('url'), or pass the url to the script at the time of initialization, for example, through the $.widget options.

A
Alexander Penshin, 2017-06-05
@apenshin

Either specify a direct path to the handler, or in php pass it to js through a variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question