2017-03-21 36 views
0

我寫了一小段JS,將數字提供給我的網站上的HTML。它工作時,我在本地運行的文件,但是當我上傳到我的主機,鋰,我的D3代碼引發此錯誤:D3 Google表格訪問控制 - 允許 - 來源錯誤

XMLHttpRequest cannot load https://docs.google.com/spreadsheets/d/12jMwpmqdbUUfcMHWg2GwGvu-d9BhaJOEsWjK1eoqHRc/pub?output=csv . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' https://alanhovorka.com ' is therefore not allowed access.

我讀過一些關於這裏的答案,但他們不」當D3從Google Sheet發佈的數據中以CSV的形式發佈時,似乎無法直接解決問題。關於CORS是否可以與D3協同工作,以及是否需要使用桌面,我見過不同的反應。我對CORS的瞭解是有限的,但它似乎是最簡單的解決方案。此外,我從來沒有能夠使桌面工作,我已經讀到它的長期生存能力的擔憂。

這裏是我與電子表格調用JS: 該網站是alanhovorka.com

d3.csv("https://docs.google.com/spreadsheets/d/12jMwpmqdbUUfcMHWg2GwGvu-d9BhaJOEsWjK1eoqHRc/pub?output=csv", function(error, data) { 
    d3.select("#requests").selectAll('h1').data(data).append("h1").style("color", "#387284").text(function(d) { 
     return d.rqt_count; 
    }); 
    d3.select("#agencies").selectAll('h1').data(data).append("h1").style("color", "#387284").text(function(d) { 
     return d.agency_cnt; 
    }); 
    d3.select("#records").selectAll('h1').data(data).append("h1").style("color", "#387284").text(function(d) { 
     return d.records_obt_cnt; 
    }); 
    d3.select("#pages").selectAll('h1').data(data).append("h1").style("color", "#387284").text(function(d) { 
     return d.pg_cnt; 
    }); 
}); 

謝謝!

回答

1

My knowledgeof CORS is limited, but it seems like it would be the simplest the solution.

它不是。您不控制Google的服務器,因此您無法將這些標題添加到回覆中。

您需要使用代理或get the data through the API來代替。

相關問題