6

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?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Nick
  • 7,475
  • 18
  • 77
  • 128

4 Answers4

7

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.

CountMurphy
  • 1,086
  • 2
  • 18
  • 39
Tim R
  • 814
  • 1
  • 5
  • 7
  • This is good if you have control over the contents of the target file. – Bruce Feb 04 '10 at 05:30
  • 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 – Nick Feb 04 '10 at 14:14
  • @user102533 Correct. You can visit json.org for a list of ASP.NET JSON serializers. – Tim R Feb 04 '10 at 22:37
4

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
  • 929
  • 6
  • 14
  • This is good if you don't have control over the contents of the target file. – Bruce Feb 04 '10 at 05:30
  • I do have control over the text file (its created by a service) – Nick Feb 04 '10 at 15:11
  • Answer is out of date. There are two direct ways to do cross-domain Ajax, and indirect ways. **1.** [JSONP](http://en.wikipedia.org/wiki/JSONP), uses ` – hippietrail Aug 24 '12 at 08:50
1

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
  • 2,858
  • 24
  • 21
  • please try again, or try this one http://github.com/jamespadolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js – Horia Dragomir Feb 04 '10 at 12:58
  • 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? – Nick Feb 04 '10 at 14:16
  • 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 – Nick Feb 04 '10 at 15:10
  • 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? – Nick Feb 05 '10 at 14:27
  • I think that's a stackoverflow question in itself – Horia Dragomir Feb 05 '10 at 17:16
0

Almost modern browsers are now supporting cross domain with CORS protocol, so you can use Ajax jQuery to do your job without editing anything in your script code. The change is into your server, you need to enable your server with CORS. It's just the job with adding header fields in each responses to client to support CORS protocol. See an implementation example here.

http://zhentao-li.blogspot.com/2013/06/example-for-enabling-cors-support-in.html

bnguyen82
  • 6,048
  • 5
  • 30
  • 39