为什么 console.log('您的查询计数:' , data);
显示为 null? (即使结果是成功?并且调试控制台网络选项卡显示数据已成功爬取)
manifest.json:
{
"title": "API test",
"server": [
{ "agent": "abc",
"url": "def" },
],
}
index.php:
<script>
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
};
getJSON('http://localhost/manifest.json',
function(err, data) {
if (err != null) {
alert('Something went wrong: ' + err);
}
else {
var read = JSON.stringify(data);
console.log('Your query count: ' , data);
}
});
</script>
最佳答案
你的代码对我有用,但请尝试
var read = JSON.stringify(data);
console.log('Your query count: ' , read );
而不是
var read = JSON.stringify(data);
console.log('Your query count: ' , data);
编辑:哦,看来您的manifest.json 不是正确的JSON。尝试删除逗号
{
"title": "API test",
"server": [{
"agent": "abc",
"url": "def"
}]
}
关于Javascript - 如何获取 list json 并将其用作对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708118/