0
林不知道這是甚至是一個淘汰賽,jquery或JavaScript的功能,但在這個網站的人給了我一個代碼來解決我的問題。他使用了我以前從未見過的.GET方法,這是代碼的一個例子。哪裏可以找到.get函數的文檔?
對於全碼:http://jsfiddle.net/rqwku4kb/
// list filter & automatic loading
self.filter = ko.observable("");
self.orderby = ko.observable("");
self.params = ko.computed(function() {
return ko.toJS({
$filter: self.filter,
$orderby: self.orderby
});
}).extend({
rateLimit: 100
});
self.params.subscribe(self.load, self);
IncidentList.prototype.load = function(params) {
var self = this;
return $.get(Incident.BASE_URL, params).then(function(data) {
if (data.d.results) {
self.incidents(data.d.results.map(function(item) {
return new Incident(item);
}));
} else {
console.log("no results received from server");
}
}).fail(function() {
console.log("error", arguments);
});
};
現在,這個代碼從服務器獲取數據。但是在XML返回它時,我所期待的JSON。所以它返回錯誤,因爲data.d.results劑量存在。我更熟悉Knockout $ .getJSON或.ajax方法,我可以手動添加標題,這樣可以返回我以前使用的JSON數據。
你們會推薦什麼,報廢函數,只需使用$ .getJSON/AJAX,或者有沒有一種方法可以用.get包含頭文件?