gpt4 book ai didi

javascript - 使用 JSON 数据时出现未定义错误

转载 作者:行者123 更新时间:2023-12-01 00:44:42 25 4
gpt4 key购买 nike

如果我使用以下代码来获取 JSON 文件:

const requestURL =
"https://securitynews.s3.eu-west-2.amazonaws.com/testchartdata.json";
const request = new XMLHttpRequest();
request.open("GET", requestURL);
request.send();`

`request.onreadystatechange = function() {
if (request.readyState === 4) {
var chartObject = request.response;
console.log(chartObject);
}
};`

当我收到错误消息“chartObject 未定义”时,如何在代码中使用 chartObject 对象,即 chartObject.companies

我想在这里使用图表对象数据:

labels: [chartObject.companies.one, chartObject.companies.two, chartObject.companies.three, chartObject.companies.four, chartObject.companies.five, ],

对于上下文,这是脚本的全部内容:

    <script>

const requestURL =
"https://securitynews.s3.eu-west-2.amazonaws.com/testchartdata.json";
const request = new XMLHttpRequest();
request.open("GET", requestURL);
request.send();

request.onreadystatechange = function() {
if (request.readyState === 4) {
var chartObject = request.response;
console.log(chartObject
);
}
};

var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [chartObject.companies.one, chartObject.companies.two, chartObject.companies.three, chartObject.companies.four, chartObject.companies.five, ],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>

最佳答案

您刚刚问过同样的问题还是其他人问过? 😅

您向 XMLHttpRequest 提供回调,因此当调用完成时,您的回调会处理响应。

const requestURL =
"https://securitynews.s3.eu-west-2.amazonaws.com/testchartdata.json";
const request = new XMLHttpRequest();
request.open("GET", requestURL);
request.send();

request.onreadystatechange = function() {
if (request.readyState === 4) {
doStuffWithResponse(request.response);
}
};

function doStuffWithResponse(chart) {
console.log(JSON.parse(chart).companies)
/*var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: [
chartObject.companies.one,
chartObject.companies.two,
chartObject.companies.three,
chartObject.companies.four,
chartObject.companies.five
],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2],
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)"
],
borderColor: [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)"
],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
}
}
});*/
}

关于javascript - 使用 JSON 数据时出现未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57486367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com