Safari jquery ajax redirect

I am making a $ .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 correctly - for each of my ajax answers, success is called. However, in safari and opera, "success" is never called, only "complete." The network query console gives me the following information:

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

Where 1717995.json is a redirect sent with permission. json I am not sure why the request is canceled (as seen from the answer).

Can anyone help?

+4
source share
1 answer

IMHO this is a cross-domain (origin) problem. By default, the browser does not execute cross-browser ajax requests. You should try using jsonp instead of json:

 dataType:"jsonp" 

but this will only work if the jsonp server is supported (in this case, you will also need to specify the name of the callback function).

If jsonp is not supported, you can make a proxy server from your server. In principle, this is not even necessary. Instead of redirecting, simply β€œdownload” the file from a third-party server and output it as an answer.

If it’s normal that this will only work in newer browsers, you can try this (this is the best solution and then the β€œproxy” on the server side is β€œIMHO”)

+1
source

Source: https://habr.com/ru/post/1416092/


All Articles