Explorer 11中的JSON.stringify

时间:2016-04-26 19:18:24

标签: javascript json google-chrome internet-explorer ecmascript-6

我有一行代码会在Explorer 11中崩溃我的整个JavaScript文件,但在Google Chrome 49.0.2623中完美运行。 我使用以下代码通过AJAX导出'javascript对象。

在Chrome中:

function AjajPostComponentLists()

{

  var myFileContent = new Uint8Array();

   var aList1 = {FixedItems:1, List:["None", "Relay #1", "Relay #2"]};
   var aList2 = {FixedItems:1, List:["None", "Input #1", "Input #2", "Input #3", "Input #4", "Input #5"]};
   var aList3 = {FixedItems:1, List:["None", "Door #1", "Door #2"]};

   // ... (I won't list them all)

   myFileContent = JSON.stringify({aList1, aList2, aList3});

   // ...
}

结果如下:

{"aList1":{"FixedItems":1,"List":["None","Relay #1","Relay #2"]},"aList2":{"FixedItems":1,"List":["None","Input #1","Input #2","Input #3","Input #4","Input #5"]},"aList3":{"FixedItems":1,"List":["None","Door #1","Door #2"]}}

Explorer 11抱怨:

  

SCRIPT1003:':'attendu(等待我猜,我的探险家是法语)

无论如何,错误指的是三元运算符,显然是无关的。

我试图放置方括号而不是花括号,并且它“通过”,除非我在过程中松散了很多(例如对象名称)。

任何线索为什么这样可以在Chrome上运行但不是IE?

由于

3 个答案:

答案 0 :(得分:2)

问题是IE doesn't support ES6对象文字扩展即:

var text = 'some text';
var obj = { text };
console.log(obj); // { text: 'some text' }

要解决此问题,您必须声明相应变量的属性名称:

myFileContent = JSON.stringify({
    aList1: aList1,
    aList2: aList2,
    aList3: aList3
});

但为了让这更容易(因为你有很多aListX个变量),让我们这样做:

// Instead of aList1 = x, aList2 = y, let's make an array of lists
var aList = [];
aList.push({FixedItems:1, List:["None", "Relay #1", "Relay #2"]});
aList.push({FixedItems:1, List:["None", "Input #1", "Input #2", "Input #3", "Input #4", "Input #5"]});
aList.push({FixedItems:1, List:["None", "Door #1", "Door #2"]});

// ...

var aListObj = {};
for (var i = 0; i < aList.length; i++) {
    aListObj['aList' + (i + 1)] = aList[i];
}

myFileContent = JSON.stringify(aListObj);

瞧!现在你不必输入最终对象中的每一个,只需循环遍历数组。输出将符合要求。

答案 1 :(得分:0)

感谢您的反馈意见,这大大改善了我的代码 plus ,让IE再次运作。我在这里总结一下:

  1. 创建对象的全局数组:
  2. var aEditableObjects = {     Relay:{FixedItems:1,List:[“None”,“Relay#1”,“Relay#2”]},     输入:{FixedItems:1,List:[“None”,“Input#1”,“Input#2”,“Input#3”,“Input#4”,“Input#5”]},     门:{FixedItems:1,List:[“None”,“Door#1”,“Door#2”]} };

    1. 对其进行字符串化,通过AJAX发送

      myFileContent = JSON.stringify(aEditableObjects);

      myFileContent = encodeURI(myFileContent); //兼容,特殊字符

    2. 通过AJAX再次打捞

      尝试{aEditableObjects = JSON.parse(decodeURI(xmlhttp.responseText));} catch(e){AjajError(“AjajGetComponentLists”);}

答案 2 :(得分:-1)

尝试public class Functions { // output blolb sizes private static readonly int[] Sizes = { 800, 500, 250 }; public static void ResizeImagesTask( [QueueTrigger("assetimage")] AssetImage asset, string container, string name, string ext, [Blob("{container}/{name}_master.{ext}", FileAccess.Read)] Stream blobStream, [Blob("{container}")] CloudBlobContainer cloudContainer) { // Get the mime type to set the content type var mimeType = MimeMapping.GetMimeMapping($"{name}_master.{ext}"); foreach (var width in Sizes) { // Set the position of the input stream to the beginning. blobStream.Seek(0, SeekOrigin.Begin); // Get the output stream var outputStream = new MemoryStream(); ResizeImage(blobStream, outputStream, width); // Get the blob reference var blob = cloudContainer.GetBlockBlobReference($"{name}_{width}.{ext}"); // Set the position of the output stream to the beginning. outputStream.Seek(0, SeekOrigin.Begin); blob.UploadFromStream(outputStream); // Update the content type => don't know if required blob.Properties.ContentType = mimeType; blob.SetProperties(); } } private static void ResizeImage(Stream input, Stream output, int width) { var instructions = new Instructions { Width = width, Mode = FitMode.Carve, Scale = ScaleMode.Both }; var imageJob = new ImageJob(input, output, instructions); // Do not dispose the source object imageJob.DisposeSourceObject = false; imageJob.Build(); } public static void PoisonErrorHandler([QueueTrigger("webjobs-blogtrigger-poison")] BlobTriggerPosionMessage message, TextWriter log) { log.Write("This blob couldn't be processed by the original function: " + message.BlobName); } } public class AssetImage { public string Container { get; set; } public string Name { get; set; } public string Ext { get; set; } } public class BlobTriggerPosionMessage { public string FunctionId { get; set; } public string BlobType { get; set; } public string ContainerName { get; set; } public string BlobName { get; set; } public string ETag { get; set; } } 参考JSON.stringify