簡體   English   中英

動態Json Keys IE11支持

[英]Dynamic Json Keys IE11 support

如果我使用了錯誤的單詞,請提前道歉,但是我遇到的問題與IE11有關,並且無法解析動態JSON密鑰。 之所以需要動態,是因為ID的環境不斷變化。 例如,我的config.js文件中包含以下內容(縮寫)

categories: {
        clientCommon: {
            get FirstName() {
                var field = window.iqbg.common.staticStrings.fieldNotSetString;
                switch (window.iqbg.config.getCurrentEnvironment()) {
                    case 'debug':
                        field = "65638_27";
                        break;
                    case 'dev':
                        field = "341840_3";
                        break;
                }
                return field;
            },
            get LastName() {
                var field = window.iqbg.common.staticStrings.fieldNotSetString;
                switch (window.iqbg.config.getCurrentEnvironment()) {
                    case 'debug':
                        field = "65638_26";
                        break;
                    case 'dev':
                        field = "341840_2";
                        break;
                }
                return field;
            },
        }
    }

然后,在我的主要功能中,我利用以下代碼行:

  copyHHSFolders: function (firstName, lastName, clientType, dateOfBirth, callback) {
 var attributes = { [config.categories.clientCommon.FirstName]: firstName,
    [config.categories.clientCommon.LastName]: lastName, 
    [config.categories.clientCommon.DateOfBirth]: dateOfBirth };
    //turns into/IE version
    var attributes = { "341840_3": "Nick",
        "341840_2": "P",
        "341840_5": "1970-01-01" };

然后將其傳遞給ajax調用並完成操作。 在Chrome和firefox中可以正常工作,但客戶端是老式學校,並且僅支持IE11(不支持Edge,但也可以使用)

但是,IE11喜歡在加載上述腳本時拋出javascript錯誤。 我現在通過將實際項目放在方括號中進行了手動修復,但我不想每次修改代碼並在環境之間移動時都不必查找/替換。

有沒有更好的方法可以實現我的目標? 注意:讓它保持簡單,說我目前僅使用濫用Visual Studio作為簡單的文本編輯器來修改html / js,沒有像react / node / angular等這樣的東西。只有jquery / bootstrap / lodash和其他一些未成年人助手庫。

我現在沒有在做任何類型的螞蟻編譯來查找/替換變量(在以前的項目中沒有做過),但是也不確定這是否是解決方案,或者不確定是否利用.ts而不是js。 同樣,對我對這些技術的無知表示歉意,很難堅持下去。

對於正在尋求答案的其他人,azium的評論使我了解以下內容(如下)基本上,創建對象內聯的簡寫不起作用。 我確實采取了以下方法。

//below line not IE compatible
//var attributes = { [config.categories.clientCommon.FirstName]: firstName, [config.categories.clientCommon.LastName]: lastName, [config.categories.clientCommon.DateOfBirth]: dateOfBirth };
//however, this is
var attributes = {};
attributes[config.categories.clientCommon.FirstName] =  firstName;
attributes[config.categories.clientCommon.LastName] = lastName;
attributes[config.categories.clientCommon.DateOfBirth] = dateOfBirth;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM