// data=eval(("("+xhr.responseText+")"));这段代码可以,但是下面的代码不行,他会报错 data=JSON.parse(xhr.responseText); //报错: Uncaught SyntaxError: Unexpected token ] in JSON at position 557 at JSON.parse (<anonymous>) at XMLHttpRequest.xhr.onreadystatechange (ajax.html:52) xhr.onreadyst...
console.log(JSON.parse(xhr.responseText)); /* console.log(JSON.parse(xhr.responseText).data); */ } }; //xhr.open('GET','./json/plain.json',true); xhr.open('GET','./json/arr.json',true); /* xhr.open('GET','https://www.imooc.com/api/http/search/suggest?words=js',true); ...
var response = JSON.parse(xhr.responseText);// 对返回的JSON数据进行处理 } };xhr.open("GET", ...
'/data.json', true);xhr.onreadystatechange = function(){if (xhr.readyState === 4 && xhr.status === 200){var data = JSON.parse(xhr.responseText);var dataDiv = document.getElement
xhr.onreadystatechange = function(ev) { console.log(this.readyState) console.log(this.status) if (this.readyState == 4 && this.status == 200) { // 修改这里的代码 data = JSON.parse(this.responseText) for(i in data) { ul.innerHTML += "" + data[i] + "" } } } . ...
var jsonData = JSON.parse(xhr.responseText); console.log(jsonData); } }; xhr.send(); 全选代码 复制 上面的代码中,我们使用了XMLHttpRequest对象发送了一个GET请求,请求的URL是data.json。当请求成功后,我们使用JSON.parse函数将返回的JSON字符串转换为JavaScript对象,并输出到控制台上。
if (xhr.readyState == 4 && xhr.status == 200) { var data = JSON.parse(xhr.responseText); console.log(data); } }; xhr.send(); //使用Fetch API获取JSON文件 fetch('data.json') .then(response => response.json()) .then(data => console.log(data)); ...
XMLHttpRequest可以使用responseText属性来获取从服务器返回的JSON数据。然后可以使用JSON.parse()方法将JSON数据转换为JavaScript对象,以便在页面中进行处理。 示例代码如下: var xhr = new XMLHttpRequest(); xhr.open('GET', 'url_to_json_data', true); xhr.onreadystatechange = function() { if (xhr.ready...
if (xhr.status === 200) { alert(xhr.responseText); } else if (xhr.status === 404) { // 网络请求返回的状态码为 404 console.log("404 not found"); } else { console.log("其他情况"); } } }; // 定义 post 请求的参数 const postData = { ...
responseData=xhr.responseText;if(false&& JSON &&JSON.parse) { responseData=JSON.parse(responseData); }else{ responseData= eval('('+responseData+')'); } } options.callback&&options.callback(responseData); }else{ alert("Problem retrieving XML data"); ...