为什么jquery .load()不从外部网站加载文本文件? [英] Why doesn't jquery .load() load a text file from an external website?
问题描述
在下面的示例中,当我点击按钮时,它会显示已执行加载但未显示任何文字。
In the example below, when I click the button, it says "Load was performed" but no text is shown.
I在根目录中有 clientaccesspolicy.xml ,并且能够从 silverlight 异步加载相同的文件。所以我认为我也应该可以从 AJAX 访问。
I have a clientaccesspolicy.xml in the root directory and am able to asynchronously load the same file from silverlight. So I would think I should be able to access from AJAX as well.
我需要更改哪些文本的文件 http://www.tanguay.info/knowsite/data.txt 是否正确显示在#content元素中?
What do I have to change so that the text of the file http://www.tanguay.info/knowsite/data.txt is properly displayed in the #content element?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function() {
$('#loadButton').click(loadDataFromExernalWebsite);
});
function loadDataFromExernalWebsite() {
$('#content').load('http://www.tanguay.info/knowsite/data.txt', function() {
alert('Load was performed.');
});
}
</script>
</head>
<body>
<p>Click the button to load content:</p>
<p id="content"></p>
<input id="loadButton" type="button" value="load content"/>
</body>
</html>
推荐答案
Clientaccesspolicy对javascript没有影响。大多数(可能全部?)现代浏览器都会阻止您运行跨站点脚本,因为它存在安全风险。
Clientaccesspolicy has no affect on javascript. Most (maybe all?) modern browsers will prevent you from running cross-site-scripting, as it is a security risk.
您的替代方案是通过a代理该站点在您自己的网站上的文件,如 /proxy.php?loadurl=http://theothersite.com
,然后通过Javascript调用该文件,这是允许的,因为它是来自您的域名。
Your alternative is to proxy that site through a file on your own site, like /proxy.php?loadurl=http://theothersite.com
and then call that file via the Javascript, which would be allowed since it is from your domain.
这篇关于为什么jquery .load()不从外部网站加载文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!