下载一个网站';通过Javascript访问s favicon.ico
我尝试过Ajax:下载一个网站';通过Javascript访问s favicon.ico,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我尝试过Ajax: $.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:/
$.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、图像、文本等等从外部域下载任何东西
我尝试通过ajax在我的Web服务器上调用php脚本来解决这个问题:
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);
在Ajax成功的结果中,我得到了图像的二进制代码,但它似乎在某种程度上被破坏了
提前谢谢。我不确定您的意思是下载到文件中,但这可以在PHP中实现。如果需要返回JS函数的路径,可以调用此AJAX样式
$url = 'http://getfavicon.appspot.com/http://www.google.com';
$img = 'icons/favicon.ico';
file_put_contents($img, file_get_contents($url));
这对我来说很有效,火箭危险品回答说: fetchIcon.php:
$domainName = false;
if(isset($_GET['domainName'])){
$domainName = $_GET['domainName'];
}
echo file_get_contents("http://getfavicon.appspot.com/http://".$domainName, true);
显示它在没有CORS限制的情况下工作的最简单方法:
<img src="fetchIcon.php?domainName=www.google.com" />
嗯,我想你正在使它变得更复杂更简单我尝试了下面的代码,它对我有效
<img id='favicon' src='http://getfavicon.appspot.com/http://www.google.com'/>
看起来你得到的是二进制代码。你还指望什么?别以为你只能用JS就可以了。我没想到会有别的。我只想知道如何使用该文件或显示它。这样做:
(确保将$\u POST
更改为$\u GET
)。让我们保持简洁。这里不需要AJAX。非常感谢火箭危险品。这比我想象的要容易得多。在这里,为什么不发布?@user2010496:因为你不能发送带有
标记的发布请求。URL中的查询字符串是GET。问题是,我需要javascript上下文中的图标,然后CORS成了一个问题。这迫使我使用php并让Web服务器作为文件的代理。
iconUniform.map.value = THREE.ImageUtils.loadTexture("fetchIcon.php?domainName=www.google.com");
<img id='favicon' src='http://getfavicon.appspot.com/http://www.google.com'/>
document.getElementById('favicon').src="address"; //address can contain new source