4

I am making an $.ajax call to an external server. This server returns a redirect, with the redirected page returning some json. This works fine on FF and Chrome, but Safari and Opera don't like it.

Here is my $.ajax code:

$.ajax(
{
    url:url,
    dataType:"json",
    success:function(data)
    {
        console.log("success");
    },
    complete:function()
    {
        console.log("complete");
    }
});

In firefox and chrome, this works properly - 'success' is called for each of my ajax responses. In safari and opera however, 'success' is never called, only 'complete'. The network requests console gives me the following information:

resolve.json    GET 302 application/json
1817995.json    GET (canceled)  undefined

Where 1717995.json is the redirection that is sent from resolve.json. I'm not sure why the request is being canceled (as seems to be indicated by the response).

Can anyone give some assistance on this?

Jaypan
  • 141
  • 4
  • what do you mean by "external server" is this a cross domain request? – Gavriel Jun 05 '12 at 08:38
  • Yes, it's cross-domain. I'm getting JSON data from soundcloud.com. If I ping the second URL (1817995.json) directly, it works, but when it's a redirect, it doesn't work. – Jaypan Jun 06 '12 at 01:11
  • I've also just hit this issue as well: cross-domain post with a redirect. I also found it works in Chrome and IE but not Safari. This however when I know for a fact the server is correctly setting the "access-control-allow-origin: *" header on both the redirect and the end page. Anyway, JSONP worked around it thanks. – DDD Feb 04 '16 at 00:29

1 Answers1

1

IMHO it's a cross-domain (origin) problem. Your browser doesn't do cross browser ajax requests by default. You should try to use jsonp instead of json:

dataType:"jsonp"

but this will only work if there's server support for jsonp (in that case you'll also need to specify the callback function's name).

If jsonp is not supported, you can make a proxy from your server. Basically it's not even needed. Instead of redirecting just "download" the file from the 3rd party server and output it as a response.

If it's ok that this will only work on newer browsers, then you can try this (which is better solution then the server side "proxy" IMHO)

Community
  • 1
  • 1
Gavriel
  • 18,880
  • 12
  • 68
  • 105