对象中的尾随逗号

问题描述:

$.ajax({ 
    url:"test.html", 
    cache: false, 
    success: function(html){ 
     $("#results").append(html); 
    }, 
});  

最后一个键值对(success:function)末尾有一个尾随逗号。我想知道是否Internet Explorer尾随逗号。 代码在Google Chrome和Mozilla Firefox中正常工作。 但在某些情况下,我收到JavaScript错误“预期标识符,字符串或数字”。 我想知道的是,尾随逗号是导致此错误的原因。对象中的尾随逗号

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas#Browser_compatibility – yuriy636

+3

删除它,看看错误消失。 – 2017-08-02 16:32:08

这与Ajax完全没有关系。它完全是关于对象字面量的:

{ 
    url:"test.html", 
    cache: false, 
    success: function(html){ 
    $("#results").append(html); 
    }, 
} 

直到ES5,对象文本中的尾随逗号被禁止。

Internet Explorer在版本9之前不支持它们(并且可能只有当Doctype触发标准模式时)。

reference