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