-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?

3
  • 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. Commented Aug 31, 2015 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. Commented Aug 31, 2015 at 14:14
  • No such luck, as they don't support jsonp Commented Aug 31, 2015 at 14:33

1 Answer 1

6

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?

Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense. Any work arounds since they refuse to allow CORS?
@Kode yes, search is magical 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.
No jsonp support either. Very limiting API

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.