未终止的字符串常量错误仅适用于 IE 9

unterminated string constant error only with IE 9

好的,我在 IE 8 和 9 中遇到了这个错误,这是 Ajax 调用

$('#$id').ajaxForm({
beforeSend: function() {
$("#{$this->name}_div").hide();
$("#{$this->name}_message").hide(); 
$("#{$this->name}_message").show().html('<img src="$gif">');
},
success: function(response)
{
     var response = JSON.parse(response);
     if (response.error != 'undefined')
{
...
}

}

问题出在这行 JavaScript 代码中

 var response = JSON.parse(response);

我的 Javescript,坏了,脚本在其他浏览器和 Internet Explorer 10 中工作,我担心是否有人使用较旧的 IE 试图访问我的网站。这个错误有什么解决办法吗?

您为什么要自己解析 JSON? ajaxForm 为此提供了 dataType 选项:

$('#$id').ajaxForm({
    beforeSend: function() {
        $("#{$this->name}_div").hide();
        $("#{$this->name}_message").hide(); 
        $("#{$this->name}_message").show().html('<img src="$gif">');
    },
    dataType: 'json',
    success: function(response){
       if (response.error != 'undefined') {
           ...
       }
    }
});

另一方面,如果您的 JSON 中存在其他浏览器恰好可以容忍的无效内容,那么几乎唯一的解决办法就是修复您的 JSON.