设置
Backbone.js用于前端的MV *,用于依赖关系管理的RequireJS,以及用于从DB检索数据的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;
答案 0 :(得分:0)
问题是我在不同于80的端口上托管服务,我的javascript正在运行。所以,我遇到了IE7缺乏CORS支持,即使使用相同的域名,端口号也必须匹配。