我無法查看從JSONP調用返回到restful api服務的數據。我在使用杜蘭達爾。我不知道它是不是呈現,因爲我使用了錯誤的數據綁定,或者如果它是由於jsonp或如果是其他原因。如何使用jsonp調用返回的數據在Durandal中呈現視圖?
這是我在我的clients.js文件:
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: "[email protected]",
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>
感謝您的任何反饋。
UPDATE 因此,我將clients.js更改爲 that.clients.push(response.items); 並在調試器我有 客戶:數組[1] 0:未定義
你有什麼錯誤嗎? – margabit
沒有錯誤。沒有出現。 – Benutzer
對我來說看起來不錯,但我會推薦下載chrome knockout上下文調試器,它可以幫助您瞭解您綁定的內容:https://chrome.google.com/webstore/detail/knockoutjs-context-debugg/oddcpmchholgcjgjdnfjmildmlielhof?hl = en –