[Chartjs]-Why my ajax function does not work on yii2

1๐Ÿ‘

I think you have to look how the routing in yii2 works
http://www.yiiframework.com/doc-2.0/guide-structure-overview.html
or here
http://www.yiiframework.com/doc-2.0/guide-start-hello.html

you can not directly call an php file ..

for example, in javascript:

$.get('/site/user-messages', function (data, status) {

                        if (status !== 'success') {
                                console.log('ERROR: '+ status);
                                return;
                        }
                        allData = JSON.parse(data);

});

here site is the controller and user-messages is the action
in this case the prettyUrl mode is enabled in urlManager

1๐Ÿ‘

Add the content of you charData.php files in a proper action eg: inside siteController.php

 public function actionChartData() {
    your code 
    .........

    return $this->render('your_related_view', [
         /* your  vars */
        'my_var' => $my_var,
    ]);
}

the call the action i ajax using

$.ajax({
url: <?php echo   \yii\helpers\Url::to(['/site/chart-data']) ?>,
type:'POST',
data:{'trigger':'trigger'},
success: function(data) {
        ........

Leave a comment