Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让变量数据来自javascript中的url_Javascript_Variables_Url_Chart.js - Fatal编程技术网

如何让变量数据来自javascript中的url

如何让变量数据来自javascript中的url,javascript,variables,url,chart.js,Javascript,Variables,Url,Chart.js,我正在使用chart.js制作图表,但是文档显示了集成到html中的数据。我想从外部源获取数据,例如localhost:80/data。如何从url获取varbarChartData作为数据 var barChartData = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [{ label:

我正在使用chart.js制作图表,但是文档显示了集成到html中的数据。我想从外部源获取数据,例如localhost:80/data。如何从url获取varbarChartData作为数据

var barChartData = {
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
            datasets: [{
                label: 'Dataset 1',
                backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
                borderColor: window.chartColors.red,
                borderWidth: 1,
                data: [1,2,3]
            }, {
                label: 'Dataset 2',
                backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(),
                borderColor: window.chartColors.blue,
                borderWidth: 1,
                data: [4,5,6]
            }]

        };
url将返回此

{
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'Dataset 1',
            backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
            borderColor: window.chartColors.red,
            borderWidth: 1,
            data: [1,2,3]
        }, {
            label: 'Dataset 2',
            backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(),
            borderColor: window.chartColors.blue,
            borderWidth: 1,
            data: [4,5,6]
        }]

    }
您可以使用Fetch

 var barChartData = fetch('localhost:80/data')
        .then(function(response){
            return response.json();
        })
        .catch(err){
    //deal with error
    }
    
    //deal with response you can render it with a for loop
您可以使用Fetch

 var barChartData = fetch('localhost:80/data')
        .then(function(response){
            return response.json();
        })
        .catch(err){
    //deal with error
    }
    
    //deal with response you can render it with a for loop
您需要
fetch()
从url获取数据,然后将其传递到图表:

fetch('http://localhost:80/data“)//从url获取数据
.then(response=>response.json())//将响应解析为json
。然后(数据=>{
//从url获取json,使用它创建图表
控制台日志(数据);
})
.catch(e=>{
//捕捉错误
控制台日志(e);
});
例如,如果您有一个名为
createChart()
的函数,则可以如下方式传递数据:

// Replace the console.log(data) in the then() with the function
.then(createChart)

function createChart(data) {
   // Create chart here with json data
}
您需要
fetch()
从url获取数据,然后将其传递到图表:

fetch('http://localhost:80/data“)//从url获取数据
.then(response=>response.json())//将响应解析为json
。然后(数据=>{
//从url获取json,使用它创建图表
控制台日志(数据);
})
.catch(e=>{
//捕捉错误
控制台日志(e);
});
例如,如果您有一个名为
createChart()
的函数,则可以如下方式传递数据:

// Replace the console.log(data) in the then() with the function
.then(createChart)

function createChart(data) {
   // Create chart here with json data
}

它在JSON数据的第2行第5列显示:SyntaxError:JSON.parse:expected property name或“}”,JSON数据看起来像什么?您的示例无效JSONI通过将单引号替换为双引号并将引号放在其他地方来修复它。它显示:SyntaxError:JSON.parse:expected property name或在JSON数据的第2行第5列的“}”JSON数据到底是什么样的?你的例子是无效的,JSONI通过将单引号替换为双引号并将引号放在其他地方来修复它。