简体   繁体   English

如何在敲门js的HTML绑定中通过Ajax获取数据?

[英]How to get data via Ajax in an HTML binding of knockout.js?

There is a DIV-container with an HTML binding and I need to get data from the server via Ajax.get request and display that data inside the contaner. 有一个带有HTML绑定的DIV容器,我需要通过Ajax.get请求从服务器获取数据并在容器中显示该数据。

Something like this: 像这样:

<div data-bind="html: ko.computed( ... get data via ajax here ... )">
</div>

How to do that? 怎么做?

In your JavaScript code you can define an observable object, and update its value in http response: 在您的JavaScript代码中,您可以定义一个可观察对象,并在http响应中更新其值:

var data=ko.observable();

$.get(url).then(function(response){
    data(response);
});

In the html file you can bind the data to element: 在html文件中,您可以将数据绑定到元素:

<div data-bind="html: data">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM