仅 IE 9 出现未终止字符串常量错误

unterminated string constant error only with IE 9

本文关键字:终止 字符串 常量 错误 IE      更新时间:2023-09-26

好的,我在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。