json note

json兼容性

  • 对象创建

    chrome 和 ie8 以上版本可以使用 XMLHttpRequest, ie7 以下版本只能使用
    ActiveX。兼容性代码如下

1
2
3
4
5
if(window.XMLHttpRequest)
xhr = new XMLHttpRequest(); //for chrome or ie8 and gr eater
else
xhr = new ActiveXObject("Microsoft.XMLHTTP"); // for ie6 and7

  • 解析(parse)

    chrome 可用JSON.parse,IE会收到错误 JSON未定义
    ,前后加括号直接转换就行。

1
2
3
4
if(typeof(JSON)=='undefined')    
var obj = eval("(" + responseText + ")");
else
var obj = JSON.parse(responseText);

以上小坑,免跳!

2020年5月28日