-1

I have a series of endpoints that a vendor's application has open for me to get files from. If I enter these endpoints into the address bar of a browser, the file opens up, but if I try to GET them via jQuery AJAX, it fails with a cross-origin error (No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mydomain' is therefore not allowed access.). The vendor application REST Web service does not support CORS. Here is my AJAX call:

$.ajax({
    url: "http://vendorrestwebservice/endpoint",
    type: "GET",
    success: function (result) {
    console.log("downloaded file");
  },
  error: function (error) {
    console.log("Failed to download file!");
  }
});

Why does the file open when pasted into an address bar, but not when called via my GET request?

w3spi
  • 4,380
  • 9
  • 47
  • 80
Kode
  • 3,073
  • 18
  • 74
  • 140
  • 1
    Well if vendor WS doesn't have CORS enabled then I think you're out of luck. Maybe try processing WS on the server-side. – Victor Levin Aug 31 '15 at 14:13
  • Does the vendor's REST web service support AJAX-P requests? Oftentimes, these are used as a CORS-free workaround, if provided. If not, then there's not really anything you can do. – Shotgun Ninja Aug 31 '15 at 14:14
  • No such luck, as they don't support jsonp – Kode Aug 31 '15 at 14:33

1 Answers1

5

The API needs to allow your domain to access it via ajax(from another domain).

When accessing directly from a browser, you are on the correct domain... :)

You can try to proxy the ajax call like this:

Make a ajax call to your server -> your server calls the api and then returns the data

OR you could try to user JSONP. Basic example of using .ajax() with JSONP?

Community
  • 1
  • 1
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
  • That makes sense. Any work arounds since they refuse to allow CORS? – Kode Aug 31 '15 at 14:12
  • @Kode yes, search is magical http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain . Or just make a script on your end and make a curl request to the other site. – Cosmin Aug 31 '15 at 14:13
  • No jsonp support either. Very limiting API – Kode Aug 31 '15 at 14:35