dsgwii4867 2016-02-02 21:32
浏览 62

Knockout Js功能没有出现在页面上

I have a js file named admin.js which has knockout js functionality.

function AppViewModel() {
  var self = this;
  self.alldata = ko.observableArray();
  self.viewAllInvoice = function() {
    $.ajax({
        type: 'POST',
        url: BASEURL + 'index.php/main/learn_Ko/',
        contentType: 'application/json; charset=utf-8'
      })
      .done(function(invoices) {
        alert("hello");
        self.alldata.removeAll();
        $.each(invoices, function(index, invoice) {

          self.alldata.push(invoice);
        });
      })
      .fail(function(xhr, status, error) {
        alert(status);
      })
      .always(function(data) {});
  };
  self.viewAllInvoice();
}


$(document).ready(function() {
  ko.applyBindings(new AppViewModel(), document.getElementById('loanersclub_wrapper'));
});

I am trying to call the function in this page like this.

<script type="text/javascript" src="<?php echo base_url();?>js/admin.js" ></script>
<div style="margin-top:30px;" class="container" id="loanersclub_wrapper" class="wrapper">
  <h1>HELLo</h1>
</div>
<div class="table-responsive">
  <table class="table table-striped table-bordered table-condensed">
    <thead>
      <tr>
        <th class="text-center">1</th>
        <th class="text-center">2</th>
      </tr>
    </thead>
    <tbody data-bind="foreach: alldata">
      <tr>
        <td class="text-center"><span data-bind="text: $data.Loantime "></span></td>
        <td class="text-center"><span data-bind="text: $data.Amount"></span></td>
      </tr>
    </tbody>
  </table>
</div>

The thing is that, I am getting this data from a controller since I am using php, and I checked the controller does get the information as arrays, but the js file is not getting any thing, I even tried using alert, but seems like the function self.viewAllInvoice is not getting called at all.

  • 写回答

1条回答 默认 最新

  • douxian4323 2016-02-02 22:30
    关注

    You either need to set the value of BASEURL to the base url of your website, or use a relative path in your ajax call:

     $.ajax({
        type: 'POST',
        url: '/index.php/main/learn_Ko/',
        contentType: 'application/json; charset=utf-8'
      })
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大