C
C
Carlos Rodriguez2015-01-31 12:40:56
Yii
Carlos Rodriguez, 2015-01-31 12:40:56

Yii2. How to create url in js files?

How to create url in js files?
so it creates links like this www.example.com/post?id=1

$.ajax({
            url: base_url + '/post/',
            dataType: "json",
            data: {'id': id},
            success: function (response) {
                ...
            }
        });

and I need www.example.com/post/1 I
have to do like this
$.ajax({
            url: base_url + '/post/' + id,
            dataType: "json",
            success: function (response) {
                ...
            }
        });

is it possible to generate url from js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2015-01-31
@Mavpa

it is possible to transfer url from php to js through registerScript . And then in js to replace the necessary parts.
PHP:

$js = 'var myUrl = "' . Yii::app()->createUrl('bla/bla/bla', array('id' => 'REPLACE_HERE')) . '";';
Yii::app()->getClientScript()->registerScript('carramba', $js, CClientScript::POS_HEAD )

JS:
var realUrl = myUrl.replace('REPLACE_HERE', id);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question