認証を使用して非同期クロスドメイン呼び出しを行っています。
認証が完了したら、一般的な get メソッドを使用して実際のアプリケーション データを取得します。
define(['jquery'], function ($) {
return {
get: function (url, data) {
// Call to authenticate before retrieve data
$.ajax({
type: 'POST',
url: 'root/authentication',
data: { user: root: password: root },
dataType: 'json',
xhrFields: {
withCredentials: true
},
complete: function (response) {
// Call to retrieve application data
$.ajax({
beforeSend: function (xhr) {
xhr.withCredentials = true;
},
type: 'GET',
url: root + url,
xhrFields: {
withCredentials: true
},
async: true,
dataType: 'json',
crossDomain: true
});
}
});
}
};
});
上記のデータ呼び出しは、別の非ジェネリック コードによって呼び出されます。
define(['cors'],
function(cors) {
return function CustomerService() {
function getCustomers() {
return cors.get('/customers');
}
return {
getCustomers: getCustomers
};
};
})
私のノックアウトjsビューモデルでは、これを行いたい:
asyc 呼び出しが行われると、renderCustomers 関数が実行され、UI が更新されます。
$.when(customerService.getCustomers())
.done(renderCustomers)
.fail();
function renderCustomers(customers) {
// Add data to knockout observables
}
顧客を renderCustomers 関数に入れるには、何を変更する必要がありますか?
現在、顧客は未定義であり、私のajax呼び出しが約束のために正しく設定されていないためだと思います。