dtqysxw4659 2018-05-08 07:32
浏览 33

AJAX成功返回HTML文件而不是数据值?

I have this simple AJAX code to get the User Timezone whenever he is successfully login to the dashboard.

JS

<script type="text/javascript">
$(document).ready(function(){
    'use strict';
    /**
     * Getting user current timezone offset
     */
    var timezone_offset = new Date().getTimezoneOffset();

    timezone_offset = timezone_offset == 0 ? 0 : -timezone_offset;
    $.ajax({
        type: "POST",
        url: "<?php echo base_url('dashboard'); ?>",
        data: {"timezone": timezone_offset},
        cache: false,
        success: function(data){
            alert(data);
        },
        error: function(){
            console.log('error');
        }
    });

});
</script>

Controller

public function index()
{
    $data = $this->global_data->logged_user();
    $data['page_title']     = 'Dashboard';
    $data['page_directory'] = 'pages/dashboard';
    $data['greeting']       = $this->global_helpers->greeting() . ' !';

    $chart_data = array();
    $order_history = $this->trade_history->chart_data();
    foreach($order_history AS $d)
    {
        $chart_data[] = array(
            'y' => $this->global_helpers->month_name($d->month) ."' ". $d->year,
            'a' => $d->profit,
            'b' => $d->loss
        );
    }

    echo $_POST['timezone'];

    $data['chart_data'] = json_encode($chart_data);
    $this->load->view('template', $data);
}

But the problem is, when it's success why it's return the HTML header? not the data I wish to get?

enter image description here

What do I do wrong here? And I put this AJAX code in footer.php file. Sorry for this silly question but I just got stuck here.

I appreciated any kind of helps, Thanks!

Edited

Sorry for this silly post, I can't use that way, i just need to create another controller for handling any kind of AJAX value, so the problem will be fixed.

  • 写回答

1条回答 默认 最新

  • doujiayao8433 2018-05-08 12:50
    关注

    Well, you can't render HTML inside an alert anyway.

    I see that you ended up solving this issue, but, FYI, if you ever need a controller to return HTML as data, use the third parameter on view method:

    echo $this->load->view('template', $data, TRUE);

    See more at Codeigniter docs :)

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向