简体   繁体   中英

base Url in ajax to call controller action in Yii passing parameters

my js external script contains the following code

$(document).ready(function() {

        $('[id^="comment"]').click(function(e) {

            $(this).prop('disabled', true);

            if ($(this).val() == 'Save') {
                var $thisClicked = $(this);
                $.ajax({

                    type: "POST",
                    url:"/comment/create/",   

                    success: function(data) {
                        $thisClicked.after(data);
                        $(".form").slideDown(200);
                    }                          
                });
            } else {
                var $thisClicked = $(this);
                $.ajax({
                    type: "POST",
                    url:"/comment/delete/",
                    success: function(data) {
                        $thisClicked.prop('disabled', false);
                    }
                });
            }
        });
});

my question is what variable should I use to point to the right directory in Yii and how to pass , variables.

I trying to do the equivalent of

url: "<?php echo Yii::app()->createUrl('comment/create', array('post_id' => $data['id'], 'user_id'=> Yii::app()->user->id)); ?>",

Thank you for your help.

I solve this by "hiding" the url in a data attribute:

<div id="comment" data-url="<?php echo Yii::app()->createUrl('comment/create', array('post_id' => $data['id'], 'user_id'=> Yii::app()->user->id)); ?>"></div>

In your javascript code you can access it by:

$("#comment").data('url');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM