您不能仅仅因为这个
cross-origin问题而做客户端。您需要服务器端脚本来获取页面的内容。
或者 您可以使用YQL
。这样,YQL
将用作代理。
https://policies.yahoo.com/us/en/yahoo/terms/product-
atos/yql/index.htm
或者, 您可以使用https://cors-anywhere.herokuapp.com。这样,任何地方的cors都将用作代理:
例如:
$('button').click(function() { $.ajax({ url: 'https://cors-anywhere.herokuapp.com/' + $('input').val() }).then(function(data) { var html = $(data); $('#kw').html(getmetaContent(html, 'description') || 'no keywords found'); $('#des').html(getmetaContent(html, 'keywords') || 'no description found'); $('#img').html(html.find('img').attr('src') || 'no image found'); });});function getmetaContent(html, name) { return html.filter( (index, tag) => tag && tag.name && tag.name == name).attr('content');}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" placeholder="Type URL here" value="http://www.html5rocks.com/en/tutorials/cors/" /><button>Get meta Data</button><pre> <div>meta Keyword: <div id="kw"></div></div> <div>Description: <div id="des"></div></div> <div>image: <div id="img"></div></div></pre>


