//这是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"}]
不知道是哪错了,请大神解答。。。