简体   繁体   English

Safari jquery ajax重定向

[英]Safari jquery ajax redirect

I am making an $.ajax call to an external server. 我正在对外部服务器进行$ .ajax调用。 This server returns a redirect, with the redirected page returning some json. 此服务器返回重定向,重定向页面返回一些json。 This works fine on FF and Chrome, but Safari and Opera don't like it. 这适用于FF和Chrome,但Safari和Opera不喜欢它。

Here is my $.ajax code: 这是我的$ .ajax代码:

$.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. 在firefox和chrome中,这可以正常工作 - 我的每个ajax响应都会调用“成功”。 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. 其中1717995.json是从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? 任何人都可以提供一些帮助吗?

IMHO it's a cross-domain (origin) problem. 恕我直言,这是一个跨域(起源)问题。 Your browser doesn't do cross browser ajax requests by default. 默认情况下,您的浏览器不会跨浏览器ajax请求。 You should try to use jsonp instead of json: 你应该尝试使用jsonp而不是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). 但这只有在服务器支持jsonp时才有效(在这种情况下,你还需要指定回调函数的名称)。

If jsonp is not supported, you can make a proxy from your server. 如果不支持jsonp,则可以从服务器创建代理。 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) 如果可以,这只适用于较新的浏览器,那么你可以尝试这个 (这是更好的解决方案,然后服务器端“代理”恕我直言)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM