// 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...
Fetch API返回响应流。这不是JSON,因此JSON.parse()需要尝试使用它的response.json()函数而不是对其进行调用。这将返回一个Promise,该Promise会将响应的正文文本解析为JSON的结果进行解析。 三、 多种方式遍历数据 1. 使用for...in const res = JSON.parse(xhr.responseText); for (const key in res){ ...
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", ...
xhr.onreadystatechange = function () { // xhr 的 readyState 属性变为 4 if (xhr.readyState === 4) { // 网络请求返回的状态码为 200 if (xhr.status === 200) { alert(xhr.responseText); } else if (xhr.status === 404) {
SyntaxError: JSON.parse: expected double-quoted property name Javascript: var init = function () { var canv = document.getElementsByTagName("canvas")[0]; var w = canv.clientWidth; var h = canv.clientHeight; var renderer = new THREE.WebGLRenderer({ ...
parse(xhr.responseText); if(obj.code){ oDiv.innerHTML = obj.message; } else{ oDiv.innerHTML = obj.message; } } } }; xhr.send(null); }; php页面: <?php header('Content-type:text/html;charset=utf-8'); $conn = mysqli_connect('127.0.0.1', 'root', '5221107073', 'linshi')...
var jsonData = JSON.parse(xhr.responseText); console.log(jsonData); } }; xhr.send(); 全选代码 复制 上面的代码中,我们使用了XMLHttpRequest对象发送了一个GET请求,请求的URL是data.json。当请求成功后,我们使用JSON.parse函数将返回的JSON字符串转换为JavaScript对象,并输出到控制台上。
xhr.open('GET', 'data.json', true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { var data = JSON.parse(xhr.responseText); document.getElementById('name').innerHTML = data.name; document.getElementById('age').innerHTML = data.age; ...
response=JSON.parse(xhr.responseText) }else{ response=xhr.responseText } params.success&¶ms.success(response) }else{ params.error&¶ms.error(status) } } }//如果是get就把参数放在url 否则放在bodyif(params.type === 'GET') { xhr.open(params.type, params.url+ '?' + params.data,true...