Javascript 如何使用jsonp调用返回的数据在Durandal中呈现视图?
我在查看从JSONP调用返回到restful api服务的数据时遇到问题。我用的是杜兰达尔。我不确定是因为我使用了错误的数据绑定,还是因为jsonp,或者是其他原因,它没有呈现 这是我在clients.js文件中的内容:Javascript 如何使用jsonp调用返回的数据在Durandal中呈现视图?,javascript,api,jsonp,durandal,Javascript,Api,Jsonp,Durandal,我在查看从JSONP调用返回到restful api服务的数据时遇到问题。我用的是杜兰达尔。我不确定是因为我使用了错误的数据绑定,还是因为jsonp,或者是其他原因,它没有呈现 这是我在clients.js文件中的内容: define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) { return { displayName: 'Clients', clients: k
define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) {
return {
displayName: 'Clients',
clients: ko.observableArray([]),
activate: function () {
var that = this;
return http.jsonp('http://site/api/clients/1', 'jsoncallback').then(function (response) {
that.clients(response.items);
});
}
};
});
返回的数据如下所示:
{
Id: 1,
Company: "Exosis",
email: "alisasandoval@exosis.com",
phone: "+1 (824) 431-2547",
address: "234 Ruby Street, Beechmont, Connecticut, 5450",
about: "Consectetur dolore excepteur ex sit nostrud.Voluptate tempor dolore minim do aliqua duis consequat nostrud amet. Nisi consequat eu Lorem ipsum.",
clientStartDate: "1990-09-18",
isActive: true,
clientType: "multiple"
}
我不明白的是我应该如何连接(数据绑定)到返回的数据。我已经尝试了所有我能想到的变化。
这是clients.hmtl文件:
<body>
<section>
<h2 data-bind="text: displayName"></h2>
<div data-bind="foreach: clients">
<div data-bind="text: Id"></div>
<div data-bind="text: Company"> </div>
</div>
</section>
</body>
谢谢你的反馈
更新
所以我把clients.js改为
即.clients.push(response.items);
在调试器中,我有
客户端:数组[1]
0:未定义不确定数组初始化后是否可以替换。尝试使用(而不是本机的
push
)推送每个元素
您可能想等待确认,我无法立即测试替换,但我肯定会按广告要求推动工作
另外,请先使用本地数据进行测试,并在重新启用JSON-p功能后监视网络流量
当我在做的时候;如果你可以(不相关)考虑CORS。< /P>是否有任何错误?没有错误。没有显示任何内容。看起来不错,但我建议下载chrome knockout上下文调试器,该调试器将帮助您查看绑定到的内容:如果您注释掉激活函数,displayName绑定中是否会显示“客户端”?请确保
此
的上下文在激活()时正确
被调用。推送帮助。我仍然被困在一个数组中,我似乎看不到它。我想知道这是否是因为jsonp数据被包装在客户机类中。