2017-04-15 91 views
1

我使用Firefox控制檯從github頁面運行一些JS(具體地說是https://github.com/user/repo/pull/1/files)。內容安全政策阻止AJAX,但該域名是有效的

我希望能夠加載回購的文件的內容,併爲我使用

var client = new XMLHttpRequest(); 
client.open("GET", "https://github.com/user/repo/raw/master/path-to-file", true); 
client.onreadystatechange = function() { 
    alert(client.responseText + client.responseURL); 
} 
client.send(); 

的問題是,內容實際上是在https://raw.githubusercontent.com/user/repo/master/path-to-file和URL我'加載(這是github網頁中的Raw按鈕指向的那個)正在重定向到那個。

Content Security Policy: The page’s settings blocked the loading of a resource at 
https://raw.githubusercontent.com/user/repo/master/path-to-file 
(「connect-src https://github.com https://uploads.github.com 
https://status.github.com https://collector.githubapp.com 
https://api.github.com https://www.google-analytics.com 
https://github-cloud.s3.amazonaws.com wss://live.github.com」). 

有沒有辦法來加載以某種方式文件:

與控制檯Firefox的錯誤?

我想我需要從github頁面運行請求,以便即使它在私人倉庫中(因爲在發送請求時使用用戶的會話)文件是可訪問的。

我送一個網頁,它在CSP的請求,該網頁不重定向,所以我不明白爲什麼CSP應該阻止我,因爲我沒有嘗試訪問「未經授權」資源。

另一種方法是在https://github.com/user/repo/blob/master/path-to-file加載GitHub的網頁,但是這意味着我必須分析它去除所有的顯示標記,它是易碎的每一個變化,以github上可能打破腳本。

回答

-1

使用ajax,您無法向不同域上的頁面發出請求。

+0

正如你可以從發佈的代碼中看到的,我沒有向不同的域發出請求,我向'https://github.com/user/repo/raw/master/path-to -file',這與我所在的域完全相同。該頁面重定向到不同域名的事實超出了我的控制範圍 –

+0

對不起,我認爲你的頁面託管在github.io上,並且你向github.com發出了請求。因爲這些域不符合您的安全問題。 – noyanc

+0

爲了清楚起見,我添加了我提出請求的頁面,因爲它確實可能模糊了github的哪個域是由它製作的 –