我試過的Ajax:通過JavaScript
$.ajax({
type : "GET",
url : "http://getfavicon.appspot.com/http://www.google.com",
success : function(result) {
// use the .ico result somewhere
}
});
它給我的錯誤:
XMLHttpRequest cannot load http://getfavicon.appspot.com/http://www.google.com. No
'Access-Control-Allow-Origin' header is present on the requested resource. Origin
'http://localhost' is therefore not allowed access.
於是,我讓我的Apache服務器上CORS,卻發現我下載的網站需要有CORS允許。如果我明白這一點,我不能通過javascript,圖像,文本,什麼都不能從外部域下載任何東西?
我試圖通過調用我的web服務器PHP腳本通過Ajax而不是去解決這個:
var domain = "www.google.com";
$.ajax({
type : "POST",
url : "php/fetchIcon.php",
data : {
'domainName' : domain
},
success : function(result) {
// use the .ico result somewhere
}
});
fetchIcon.php:
$domainName = false;
if(isset($_POST['domainName'])){
$domainName = $_POST['domainName'];
}
echo file_get_contents("http://getfavicon.appspot.com/http://".$domainName, true);
在阿賈克斯成功結果我回來了圖像的二進制代碼,但它似乎以某種方式打破。
如果我想顯示的.ico文件,我可以這樣做: 「的document.getElementById(」 IMG 「)。SRC =結果;」 ?在我的項目中,我想使用「THREE.ImageUtils.loadTexture(result);」。但對於這個問題來說這太過分了。
我需要使用Base64編碼/解碼嗎?
有沒有更容易的方法或黑客做到這一點,只是在沒有PHP的Javascript?
在此先感謝。
似乎是正確的,你得到的二進制代碼。你還期望什麼?不要以爲你只能用JS做。 – putvande
我沒有想到別的。我只想知道如何使用該文件或顯示它 – user2010496
這是否工作:'
'(確保將'$ _POST'更改爲'$ _GET')。讓我們保持良好和簡單。這裏不需要AJAX。 –