Why doesn't jquery .load() load a text file from an external website?

南笙酒味 提交于 2019-12-09 02:07:37

问题


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. So I would think I should be able to access from AJAX as well.

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>

回答1:


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.

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.




回答2:


I don't think any browser obeys clientaccesspolicy.xml or crossdomain.xml for XMLHttpRequest.

There are other mechanisms you can look at, such as Cross-Origin Resource Sharing. This is supported by Firefox 3.5 and later.



来源:https://stackoverflow.com/questions/2734676/why-doesnt-jquery-load-load-a-text-file-from-an-external-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!