javascript - Knockout Js函数没有出现在页面上

标签 javascript php knockout.js

我有一个名为 admin.js 的 js 文件,它具有 knockout js 功能。

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'));
});

我正在尝试像这样调用此页面中的函数。

<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>

问题是,我从 Controller 获取这些数据,因为我使用的是 php,并且我检查了 Controller 确实以数组形式获取信息,但是 js 文件没有获取任何内容,我什至尝试使用警报,但似乎函数 self.viewAllInvoice 根本没有被调用。

最佳答案

您需要将 BASEURL 的值设置为网站的基本 url,或者在 ajax 调用中使用相对路径:

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

关于javascript - Knockout Js函数没有出现在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35164176/

相关文章:

php - 我如何进行 AJAX 表单例份验证?

javascript - knockout.js - 二维数组绑定(bind)问题

javascript - Knockout 间歇性地将元素与 [object HTMLDivElement] 绑定(bind)

javascript - Knockoutjs 可观察对象数组按名称绑定(bind)选择器

javascript - 如何给js和css文件添加版本号

javascript - 在 For 循环中创建数组而不运行数组

javascript - 在行尾用圆圈连接两个 div

PhpSpreadsheet 条件格式化单元格(填充)

php - 如何在 zend framework 2 中自动加载 paypal php sdk 的类文件?

javascript - 获取更改文本的元素的 outerWidth