1
我嘗試直接在Ajax調用中映射和綁定JSON數據,但沒有成功。該警報顯示所請求的數據。如果我將此警報的內容複製/粘貼到測試變量中,則映射工作正常。爲什麼我不能直接映射/綁定到結果? 'test'和'result.d'有什麼區別? (綁定結果 - 沒有.d也不起作用)。基因敲除通過Ajax調用json
感謝您的任何幫助。
var test = { "formulier": { "titel": "test", "formulier_id": 4, "vragen": [{ "vraag_id": 5, "vraag": "Hoe gaat het?", "soortvraag_id": 4, "soortwaarde_id": 13, "verplicht": false, "antwoorden": [] }, { "vraag_id": 6, "vraag": "Dit is een kopje", "soortvraag_id": 18, "soortwaarde_id": 13, "verplicht": false, "antwoorden": [] }, { "vraag_id": 3, "vraag": "vraag 3k", "soortvraag_id": 6, "soortwaarde_id": 13, "verplicht": false, "antwoorden": [{ "antwoord": "nieuw2", "antwoord_id": 7, "vraag_id": 3, "iscorrect": true }, { "antwoord": "echt waar?", "antwoord_id": 5, "vraag_id": 3, "iscorrect": false }, { "antwoord": "nieuw 123", "antwoord_id": 2, "vraag_id": 3, "iscorrect": false }, { "antwoord": "nieuw", "antwoord_id": 6, "vraag_id": 3, "iscorrect": false }] }] } }
// This doesn't work
$.ajax({
type: "POST",
contentType: "application/json",
url: "../formulierService.asmx/getLeegFormulier",
data: "{'id': " + 4 + "}",
dataType: "json",
success: function (result) {
alert(result.d); //content of alert was pasted into var test above
var ViewModel = ko.mapping.fromJS(result.d);
ko.applyBindings(ViewModel);
}
});
// This does work
var ViewModel2 = ko.mapping.fromJS(test);
ko.applyBindings(ViewModel2);
是,ko.mapping.fromJSON做了招。 – Jeroen