如何从外部网站中提取特定的div内容?

时间:2014-05-03 17:48:55

标签: javascript jquery html

我正在尝试使用Jquery .load()方法从外部网站中提取一些信息,但这似乎不起作用,任何想法为什么?我测试了firefox和chrome。

 <!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("http://www.weather.com/weather/5-day/CEXX0001 #wx-forecast-container");
  });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

2 个答案:

答案 0 :(得分:10)

会抛出错误 请求的资源上没有“Access-Control-Allow-Origin”标头

原因是您无法通过XmlHttpRequest访问外部域网址。

替代方案是创建服务器端intermediater,它从weather.com提取数据并使用load或ajax方法提供给您的页面。

答案 1 :(得分:1)

您正面临跨域政策限制。浏览器只是不允许您从外部网站加载内容, 您可以尝试在weather.com上放置crossdomain.xml,但不能