[英]Backbone.js fetch error in IE7
設置
前端MV *的Backbone.js,依賴項管理的RequireJS和從數據庫檢索數據的REST服務。 這些服務與Web應用程序位於同一域中。
問題
當我通過IE 7中的REST服務從服務器獲取集合時,將引發錯誤回調。 當我檢查xhr對象時,狀態為0,statusText為0。這僅是IE 7中的問題。FF和Chrome對ajax GET沒問題。
這是代碼(AMD定義)
define([
'underscore',
'backbone',
'models/imageLink',
'app/registry'
], function(_, Backbone, ImageLink, AppReg) {
var collection = Backbone.Collection.extend({
ajaxRequest: false,
highestKey: 0,
polling : false,
pollDelay: 7500,
// Reference to this collection's model.
model: ImageLink,
url: function(){
return "http://samedomain:4020/api/imageLinks" + "?userId=" + this.user;
},
user: "",
initialize: function() {
// ensure correct context of 'this'
_.bindAll(this, 'startPolling', 'stopPolling', 'executePolling', "onFetch");
var _this=this;
console.log('Image Links collection has been initialized.');
},
// used for sorting collection, sorts based on the lowercase value of the imageLink's text attribute
comparator: function(imageLink) {
return imageLink.get('text').toLowerCase();
},
// override parse function to prevent backbone from updating empty data returned from server
parse: function(response,options) {
//debugger;
if (options.xhr.status===204) {
return this.toJSON();
}
else
return response;
},
getHighestKey: function() {
if (this.length) {
return this.at(this.length-1).get("id");
}
else {
return this.highestKey;
}
},
startPolling : function() {
this.polling = true;
this.highestKey = this.getHighestKey();
this.executePolling();
},
stopPolling : function() {
this.polling = false;
},
executePolling : function() {
if (this.ajaxRequest == "") {
this.ajaxRequest = this.fetch({ reset:true, complete: this.onFetch, timeout: 30000, data: $.param({ key: this.highestKey }),
error: function(model, xhr, options) {
alert("Error\n"+xhr.readyState+"\n"+xhr.status+"\n"+xhr.statusText.toString()+"\n"+xhr.responseText);
}
});
}
},
onFetch : function () {
this.ajaxRequest = "";
this.highestKey = this.getHighestKey();
if( this.polling ) {
// poll database
setTimeout(this.executePolling,this.pollDelay);
}
}
});
return collection;
});
注意
使用IE 9中的開發人員工具在IE 7下進行渲染時,該頁面工作正常。僅當獨立加載到IE 7中時,該問題才會出現。
另外,我在應用啟動時執行以下操作:
// do not cache any ajax requests
$.ajaxSetup({ cache: false });
// needed for IE CORS support
$.support.cors = true;
問題是我在運行JavaScript的端口而不是80上托管該服務。 因此,我遇到了IE7缺少CORS支持的問題,即使具有相同的域,端口號也必須匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.