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'
      })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆