C
C
Carlos Rodriguez2015-01-16 12:00:38
Yii
Carlos Rodriguez, 2015-01-16 12:00:38

Yii2. How to generate url in javascript code?

when I specify the url manually, it works on the main page,
but on others, instead of sending a request without reloading, it goes to the specified url

<?php $js = <<<JS
            jQuery('#forgot-password').on('click', function(){
                jQuery.post('site/request-password-reset')
                    .done(function(result) {
                      ....
                    })
                return false;
            })
JS;
        $this->registerJs($js);
        ?>

1) how to generate url for such a case?
2) how to generate url in *.js file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Carlos Rodriguez, 2015-01-16
@asf

Decided by adding to the layout

<script>
        var base_url = '<?= \Yii::$app->request->hostInfo . "/" . \Yii::$app->language; ?>';
</script>

well, then I glue this url to the controler / action structure in js
ps maybe someone knows the best option will be glad to hear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question