Internet explorer 8 IE8的淘汰赛,Stringify的偶尔问题?
我们的许多用户仍然使用IE8。他们中的一些人在试图将数据发布到我们的服务器时(通过一个标有“保存”的大按钮)偶尔会报告问题 IE8显示了一个脚本错误,即:对方法或属性访问的意外调用,始终指向KnockoutJS 2.2.0(目前为调试)库中的同一行,第450行,如下所示:Internet explorer 8 IE8的淘汰赛,Stringify的偶尔问题?,internet-explorer-8,knockout.js,stringify,Internet Explorer 8,Knockout.js,Stringify,我们的许多用户仍然使用IE8。他们中的一些人在试图将数据发布到我们的服务器时(通过一个标有“保存”的大按钮)偶尔会报告问题 IE8显示了一个脚本错误,即:对方法或属性访问的意外调用,始终指向KnockoutJS 2.2.0(目前为调试)库中的同一行,第450行,如下所示: return JSON.stringify(ko.utils.unwrapObservable(data), replacer, space); 我的代码中发生这种情况的堆栈跟踪根的方法如下: self.saveSingle
return JSON.stringify(ko.utils.unwrapObservable(data), replacer, space);
我的代码中发生这种情况的堆栈跟踪根的方法如下:
self.saveSingle = function (onSuccess, onFailure) {
ko.utils.arrayForEach(self.days(), function (day) {
day.close();
});
var jsonData = ko.toJSON(self);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: applicationLocation + "/api/assignmentapi/save",
data: jsonData,
success: function (data) {
self.status(data.Status);
self._isDirty(false);
ko.utils.arrayForEach(self.days(), function (day) {
day.clean();
});
if (onSuccess)
onSuccess();
},
error: function (data) {
onFailure();
},
dataType: "json"
});
};
当我们使用以下方法将对象转换为JSON时,我们确实去掉了许多对我们的文章不必要的属性:
当它失败时,它会在线路上持续失败
var jsonData = ko.toJSON(self);
现在真正的麻烦来了:
我不知道这是否能解决问题,但您可以使用以下命令在JS和JSON之间切换:
var mapping = {
'ignore': ["propertyToIgnore", "alsoIgnoreThis"]
}
var viewModel = ko.mapping.toJS(data, mapping);
从我的
我会尝试一下,看看它是否有用,因为你的方法没有明显的错误。你确定是IE8用户遇到了这个问题吗。您需要包括以支持IE7和更低版本。我也遇到了这个问题。深入挖掘,我发现了一些东西:
- 只是偶尔会失败,我通过在控制台中运行代码发现了这一点
中的代码出现异常,但由于IE8在使用数据绑定
块(无try{}finally{}
)时吞没了消息,消息被吞没捕获
- 删除try最终显示无法解析绑定消息
ko.utils.extend(ko.bindingProvider.prototype, {
'nodeHasBindings': function(node) {
switch (node.nodeType) {
case 1: return node.getAttribute(defaultBindingAttributeName) != null; // Element
case 8: return ko.virtualElements.virtualNodeBindingValue(node) != null; // Comment node
default: return false;
}
},
'getBindings': function(node, bindingContext) {
var bindingsString = this['getBindingsString'](node, bindingContext);
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext, node) : null;
},
// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'getBindingsString': function(node, bindingContext) {
switch (node.nodeType) {
case 1: return node.getAttribute(defaultBindingAttributeName); // Element
case 8: return ko.virtualElements.virtualNodeBindingValue(node); // Comment node
default: return null;
}
},
// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'parseBindingsString': function(bindingsString, bindingContext, node) {
try {
var bindingFunction = createBindingsStringEvaluatorViaCache(bindingsString, this.bindingCache);
return bindingFunction(bindingContext, node);
} catch (ex) {
throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
}
}
});
但是,是的,它不再是可复制的,所以我想出了一个我早先测试和工作的方法,只是重新尝试数据解析。因此:
data-bind="value: ko.computed(function(){return ko.toJSON(appViewModel.model()[0])})"
变成这样:
data-bind="value: ko.computed(function(){while (true) { try { var json = ko.toJSON(appViewModel.model()[0]); return json; }catch(e){}}})"
是的,这很恶心,但它似乎起到了作用,直到我们的用户不再需要IE8或者淘汰问题得到解决。我们为IE7用户提供了json2。没有IE7用户报告过问题;只有IE8用户。很有趣。可能是IE8用户实际上是在IE7模拟模式下运行的(IE页面兼容性)?我们的QA人员有时能够复制它,并且当他们复制时是在IE8模式下。哇,这真的很奇怪。如果为IE8用户提供json2库,它会有什么变化吗?它实际上通常包含在我们的站点脚本中,但由于它会在内部检查JSON是否已定义,因此限制了它的用途。好吧,这很讨厌。:)我们现在已经克服了这一点,取而代之的是直接在对象中创建我们自己的toJSON方法。不利的一面是,重新实现本应该有效的东西感觉是错误的,但既然我们现在创建了自己的,我就不再需要删除我不关心的属性了。我想是把双刃剑吧。
data-bind="value: ko.computed(function(){while (true) { try { var json = ko.toJSON(appViewModel.model()[0]); return json; }catch(e){}}})"