使用 AJAX 从特定外部 DIV 加载文本?
我正在尝试从 http://www.census 加载估计的世界人口。 gov/ipc/www/popclockworld.html 使用 AJAX,到目前为止,失败得很惨。
该页面上有一个 ID 为“worldnumber”的 DIV,其中包含估计人口,因此这是我想从该页面获取的唯一文本。
这是我尝试过的:
$(document).ready(function(){
$("#population").load('http://www.census.gov/ipc/www/popclockworld.html #worldnumber *');
});
I'm trying to load up the estimated world population from http://www.census.gov/ipc/www/popclockworld.html using AJAX, and so far, failing miserably.
There's a DIV with the ID "worldnumber" on that page which contains the estimated population, so that's the only text I want to grab from the page.
Here's what I've tried:
$(document).ready(function(){
$("#population").load('http://www.census.gov/ipc/www/popclockworld.html #worldnumber *');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试执行的操作称为跨域请求。这不是浏览器通常允许的功能(安全功能)。这里描述了一些绕过这个限制的方法: The jQuery Cross - 域 Ajax 指南。
What you are trying to do is known as a cross-domain request. This is not a feature that browsers normally allow (security feature). Some ways to get around this limitation are described here: The jQuery Cross-Domain Ajax Guide.
你可以尝试这样的事情:
you can try something like this:
是的,这就是安全。您无法使用 ajax 访问非同一域的页面。
Yeah, it's security. You can't ajax in to pages that aren't from the same domain.
@R0MANARMY:
我似乎无法遵循您链接到的该网站上给出的说明,但我确实找到了解决方案...我使用以下代码创建了一个 PHP 文件:
然后我调用像这样使用 jQuery:
只是想我会将其发布在这里,以防其他人想做类似的事情。
@R0MANARMY:
I couldn't seem to follow the directions given on that site you linked to, but I did figure out a solution... I created a PHP file with the following code:
Then I called it with jQuery like so:
Just thought I'd post it here in case anyone else is looking to do something similar.