简体   繁体   English

为什么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 have a clientaccesspolicy.xml in the root directory and am able to asynchronously load the same file from silverlight . 我在根目录中有一个clientaccesspolicy.xml ,并且能够从silverlight异步加载相同的文件。 So I would think I should be able to access from AJAX as well. 因此,我认为我也应该能够从AJAX进行访问。

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? 我必须更改什么,才能在#content元素中正确显示文件http://www.tanguay.info/knowsite/data.txt的文本?

<!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 has no affect on javascript. Clientaccesspolicy对javascript没有影响。 Most (maybe all?) modern browsers will prevent you from running cross-site-scripting, as it is a security risk. 大多数(也许是所有?)现代浏览器都会阻止您运行跨站点脚本,因为这存在安全风险。

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. 您的替代方法是通过自己站点上的文件(例如/proxy.php?loadurl=http://theothersite.com代理该站点,然后通过Javascript调用该文件,这是允许的,因为它来自您的域。

I don't think any browser obeys clientaccesspolicy.xml or crossdomain.xml for XMLHttpRequest. 我不认为任何浏览器都遵循XMLHttpRequest的clientaccesspolicy.xmlcrossdomain.xml

There are other mechanisms you can look at, such as Cross-Origin Resource Sharing . 您还可以查看其他机制,例如“ 跨域资源共享” This is supported by Firefox 3.5 and later. Firefox 3.5和更高版本支持此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM