views:

270

answers:

3

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <script type="text/javascript"
        src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
        <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  A: 

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.

Matthew Flaschen
I also put a crossdomain.xml in the root directly, still doesn't read any text.
Edward Tanguay
Crossdomain is for flash. No file you put anywhere will allow a javascript from another site to load from a script call.
webdestroya
+3  A: 

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.

webdestroya
I tried it with this http://www.tanguay.info/web/getdata/index.php?url=http://www.tanguay.info/knowsite/data.txt which internally gets text via PHP/CURL and works in silverlight for any site without clientaccesspolicy.xml, so it should work here. How do I debug this? In Firebug I see that .load() is being executed but not what it is returning, what error, etc. :-(
Edward Tanguay
Using a proxy script like that will work in anything, but why not just add a `console.log` statement to see what is being echoed? And firebug **will** tell you all the HTTP requests and their responses. As long as you have the console open.
webdestroya
thanks for the console.log tip, but where in firebug do I get the http request/response/error information, posted a specific question on this: http://stackoverflow.com/questions/2734887/how-to-get-firebug-to-tell-me-what-error-jquerys-load-is-returning
Edward Tanguay
Firebug should show you the content in the **Net** tab
webdestroya
I like this solution, how it calls another php program within the domain of the original script to load in the page you want, without getting javascripts cross site errors.For Javescript to access a file in tanguay.info/web/getdata/ it must be called from within domain tanguay.info/ or it will generate a xss error.Oh and jquery is up to version 1.4.2, it might have some bug fixes?
Hellonearthis
+1  A: 

Instead do a request for the text via a script tag:

$('<script type="text/html"></scr'+'ipt>')
   .attr('src', url_to_textfile)
   .appendTo('body').load(function(e){

    $('#content').html($(this).html());
    $(this).remove();

});

This is a poorman's jsonp, which would be the actual best choice for this, if you have control over the other resource.

Alex Sexton
Nice!!!!!!!!!!!
Strelok
Save some exclamation marks for the next generation!
Alex Sexton