2 sosomatter sosomatter 于 2014.09.30 22:34 提问

关于callback(eval('('+xhr.responseText+')'));IE报错的问题

//这是index.js的文件
(function() {
var li = function(id) {
return document.getElementById(id);
};

li.init = function() {// li下的init方法
    // 创建一个ajax对象并返回
    try {
        return new XMLHttpRequest();
    } catch (e) {
        // null
    }
    try {
        return new ActiveXObject('Microsoft.XMLHTTP');
    } catch (e) {
        // null
    }
    alert('浏览器不支持ajax!')
    return;

};

li.get = function(url, data, callback, type) {
    // 以get形式发送ajax
    var xhr = li.init();
    if (data != null) {
        url = url + '?' + data;
    }
    xhr.open('get', url);
    xhr.setRequestHeader("if-Modified-Since", "0");// 去除get缓存
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if (type == null) {
                type = 'text';
            }
            if (type == 'text') {
                callback(xhr.responseText);
            }
            if (type == 'xml') {
                callback(xhr.responseXML);
            }
            if (type == 'json') {
                callback(eval('(' + xhr.responseText + ')'));
            }
        }
    };
    xhr.send(null);
};

li.post = function(url, data, callback, type) {
    // 以post形式发送ajax
    var xhr = li.init();
    xhr.open('post', url);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if (type == null) {
                type = 'text';
            }
            if (type == 'text') {
                callback(xhr.responseText);
            }
            if (type == 'xml') {
                callback(xhr.responseXML);
            }
            if (type == 'json') {
                callback(eval('('+xhr.responseText+')'));//这个地方出错。
            }
        }
    };

    xhr.send(data);
};

li.addEvent=function(id,name,callback){

}


window.$ = li;// 等于创建一个全局名为$的变量

})();

//在callback(eval('('+xhr.responseText+')'));这个地方IE报错,

我的json传值的格式是:
Array[{"id":"3","sendId":"1","destId":"0","content":"","mtime":"2014-09-27
21:49:39","name":"admin"},{"id":"4","sendId":"1","destId":"0","content":"ddddddddddddd","mtime":"2014-09-27
21:50:50","name":"admin"},{"id":"5","sendId":"1","destId":"0","content":"dddddddddddddddddddddddddddddddd","mtime":"2014-09-27
21:50:57","name":"admin"},{"id":"6","sendId":"1","destId":"0","content":"dwwwwwwwwwadadawdawda","mtime":"2014-09-27
21:51:04","name":"admin"},{"id":"7","sendId":"1","destId":"0","content":"wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww","mtime":"2014-09-27
21:51:24","name":"admin"},{"id":"8","sendId":"1","destId":"0","content":"dddddddddddddddddddwdwd","mtime":"2014-09-27
21:51:31","name":"admin"},{"id":"9","sendId":"1","destId":"0","content":"dddddddddddddddddddddddddd","mtime":"2014-09-28
21:24:19","name":"admin"},{"id":"10","sendId":"1","destId":"0","content":"dddddddddddd","mtime":"2014-09-28
22:26:05","name":"admin"},{"id":"11","sendId":"1","destId":"0","content":"ddddd","mtime":"2014-09-29
22:07:41","name":"admin"}]

不知道是哪错了,请大神解答。。。

2个回答

showbo
showbo   Ds   Rxr 2014.11.19 10:37

去掉Array,要不eval后得到的返回为undefined,你的callback调用后就会出错

baidu_35140466
baidu_35140466   2016.05.27 17:53

请问楼主的问题解决了么 我在自学中 也是这个错误 代码基本跟你一样的也是这行代码一直报语法错误

Csdn user default icon
上传中...
上传图片
插入图片