gpt4 book ai didi

internet-explorer-8 - KnockoutJS 与 IE8,Stringify 偶尔出现问题?

转载 作者:行者123 更新时间:2023-12-04 00:12:43 26 4
gpt4 key购买 nike

我们的许多用户仍在使用 IE8。他们中的一些人在尝试将数据发布到我们的服务器时偶尔会报告问题(通过标有“保存”的大按钮)。

IE8显示有一个脚本错误,即:Unexpected call to method or property access,始终指向KnockoutJS 2.2.0(debug,for now)库中的同一行,第450行,如下:

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 时,我们确实去除了一些 POST 不需要的属性: http://www.knockmeout.net/2011/04/controlling-how-object-is-converted-to.html
OurType.prototype.toJSON = function () {
var copy = ko.toJS(this);

delete copy.someUnneededProperty1;
delete copy.someUnneededProperty2;
delete copy.someUnneededProperty3;
delete copy.someUnneededProperty4;

return copy;
}

当它失败时,它始终在线上失败
var jsonData = ko.toJSON(self);

现在真正的困惑来了:
  • 它不会一直发生
  • 并非所有 IE8 用户都会发生这种情况
  • 我们无法始终如一地重现它
  • 我们正在序列化的模型的结构似乎无关紧要
  • jscript.dll 是 IE8 的当前版本
  • 最佳答案

    我也遇到了这个问题。深入挖掘我发现了一些东西:

  • 它只是偶尔失败,我通过在控制台中运行代码发现了这一点
    enter image description here
  • data-bind 中的代码抛出异常,但由于 IE8 在使用 try {} finally {} 时吞噬了消息而导致消息被吞下。 block (没有 catch )。
  • 删除尝试最终显示无法解析绑定(bind)消息。

  • 当我开始接近解决问题(深入挖掘 knockout 代码)时,它似乎在我眼前消失了。这是它失败的代码部分,在代码末尾捕获异常:
    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);
    }
    }
    });

    但是,是的,它不再是可重现的了,所以我想出了一个我之前测试和工作的 hack,只是重试数据解析。所以这:
    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 或修复了 Knockout 问题。

    关于internet-explorer-8 - KnockoutJS 与 IE8,Stringify 偶尔出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15206398/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com