views:

538

answers:

3

I have this polling script to check if a text file is created on the server. Works great locally, but fails when the file is on a different domain. How would i rewrite this for cross domain support?

$.ajax({ 
    url: 'http://blah.mydomain.com/test.txt', 
    type: "GET", 
    success: function(result) { 
        //Success!
        window.location.replace(Successful.aspx');
    }, 
    error: function(request, status, error) { 
        setTimeout("VerifyStatus(" + pollingInterval + ")");
    }
    });

EDIT: I ended up using YQL to solve the cross domain issue and although it works, YQL is really slow that's adding quite a bit of performance overhead. Can anyone suggest a better solution for cross domain JQuery calls?

+5  A: 

Ajax doesn't go cross domain. Your best bet is to create a php page on the local domain that does the check, and go to -that- with your ajax call.

monksp
This is good if you don't have control over the contents of the target file.
Bruce
I do have control over the text file (its created by a service)
A: 

To get cross-domain AJAX via jQuery, you might want to check this out: http://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

Horia Dragomir
I get a 404 on that link
please try again, or try this one http://github.com/jamespadolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js
Horia Dragomir
So, all I have to do is add jquery.xdomainajax.js to the project and not make any changes to the way the ajax call is made?
that is correct.
Horia Dragomir
this looks very promising.. i will try it and let you know how it goes.. I assume it does not modify the way ajax calls are made for calls that are non cross domain
nope, same domain requests will not be affected.
Horia Dragomir
The plugin does not work for me. I can't catch the fail event and retry. I believe this is by design. Any way of getting around this issue?
I think that's a stackoverflow question in itself
Horia Dragomir
+3  A: 

Set the dataType to "JSONP" on your $.ajax() call. You'll have to make sure the response is properly formatted for it to work. Wikipedia has a good section on JSONP.

Tim R
This is good if you have control over the contents of the target file.
Bruce
I do have control over the creation of the txt file. With jsonp, do I have to write a JSON string in the text file? I am using ASP.NET and have trouble putting it all together
@user102533 Correct. You can visit json.org for a list of ASP.NET JSON serializers.
Tim R