通过JQuery获取另一个网页的html源/文本
是否可以:
- 从我的网页内,从另一个网页获取所有 HTML 源代码。我需要这样做才能将其他页面 html 源传递给我的函数。
以下不同的尝试不起作用:
var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();
var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/");
var html = $("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();
Is it possible to:
- From within my webpage, get all the HTML source from another webpage. I need to do this in order to pass that other pages html source to my function.
The following different attempts don't work:
var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();
var html = $.get("http://simplyrecipes.com/recipes/braised_turkey_legs/");
var html = $("http://simplyrecipes.com/recipes/braised_turkey_legs/").html();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用Ajax,而get只是ajax的快捷方式,实际上不可能从其他域获取源html,如果这就是你想要做的?出于安全原因,ajax 有同源策略。
然而,通过 YQL 可以做的是,阅读 this 看看如何,或者您可以使用 php 或其他东西进行代理,仅使用常规的 get 请求,但这是不可行的。
如果您尝试获取的页面位于您的域中,则 .load();可能会更好。
Using Ajax, and get is just a shortcut for ajax, it's not really possible to get the source html from other domains, if that is what your trying to do? as ajax has a same origin policy for security reasons.
However going thru YQL it is possible to do is, read this to see how, or you could proxy with php or something else, with just regular get requests however it's not doable.
If the pages you are trying to get are on your domain, .load(); would probably be better.
Javascript 有一个同源策略,这就是阻碍你的因素。
Javascript has a same origin policy, this is what's holding you back.
jQuery 的 get 函数不会返回加载的数据,而是调用回调函数并将数据作为参数传递。
这是来自 文档:
PS:请注意,无论如何,这仅适用于相同的情况域,因为出于安全原因通常不支持跨域 AJAX。
jQuery's
get
function does not return the loaded data - rather, it calls a callback function and passes the data as a parameter.This is from the documentation:
P.S.: Note that in any case this will only work on the same domain, as cross-domain AJAX is normally not supported for security reasons.