IE8+只有在xhtml模式下才支持JSON对象

  标准浏览器如chrome,firefox都支持JSON对象,将json对象转为对应json格式的字符串(JSON.stringify)或者将json格式的字符串转为json对象(JSON.parse)。

  IE8+浏览器也支持JSON对象,但是是有条件的,需要添加xhtml申明或者使用X-UA-Compatible指定为IE8或者edge模式,X-UA-Compatible要放对位置,要不有可能无效。参考:IE X-UA-Compatible meta失效分析


  不过IE8+下JSON.stringify会将中文转为unicode编码(\uxxxx这种编码),所以还是推荐使用json2.js在IE浏览器下实现json对象的序列化与反序列化。


怪异模式

<script>
    alert(typeof JSON)//输出undefined
</script>


xhtml模式

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script>
    document.write(JSON.stringify({ d: 'JSON对象' }))//输出{"d":"JSON\u5bf9\u8c61"},中文被转为unicode编码
</script>

使用X-UA-Compatible

<meta http-equiv="X-UA-Compatible" content="IE=8">
<script>
    alert(typeof JSON)
</script>

或者

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script>
    alert(typeof JSON)
</script>

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:IE8+只有在xhtml模式下才支持JSON对象

评论(0)Web开发网
阅读(459)喜欢(0)JavaScript/Ajax开发技巧