为什么jquery .load()不从外部网站加载文本文件?
在下面的示例中,当我单击按钮时,它显示“已执行加载”但未显示任何文本 。
我在根目录中有一个clientaccesspolicy.xml ,并且能够从silverlight异步加载相同的文件。 所以我认为我也应该可以从AJAX访问。
我需要更改什么才能在#content元素中正确显示文件http://www.tanguay.info/knowsite/data.txt的文本?
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.'); }); } Click the button to load content:
Clientaccesspolicy对javascript没有影响。 大多数(可能全部?)现代浏览器将阻止您运行跨站点脚本,因为它存在安全风险。
您可以选择通过您自己网站上的文件代理该网站,例如/proxy.php?loadurl=http://theothersite.com
,然后通过Javascript调用该文件,因为它来自您的域名,因此将被允许。
我不认为任何浏览器都遵循XMLHttpRequest的clientaccesspolicy.xml
或crossdomain.xml
。
您可以查看其他机制,例如跨源资源共享 。 Firefox 3.5及更高版本支持此function。