对重定向到另一个域的域的jQuery ajax调用(不跟随)

时间:2022-08-23 10:37:06

This is sort of a cross-domain issue, but the problem is the browser (Chrome) doesn't seem to follow the redirect. Instead, nothing is returned to the jQuery ajax call, and I get an error.

这是一个跨领域的问题,但问题是浏览器(Chrome)似乎没有遵循重定向。相反,jQuery ajax调用没有返回任何内容,我得到一个错误。

I'm trying to use jQuery.ajax, but the URL that I'm using redirects to another domain. When this happens, I get an error. Is there anything special that needs to be done so the browser will follow the redirect?

我在尝试使用jQuery。ajax,但是我使用的URL重定向到另一个域。当这种情况发生时,我得到一个错误。有什么特别的事情需要做,以便浏览器遵循重定向吗?

I already added access-control-allow-origin: * to the header of the second domain that is being redirected to.

我已经向被重定向到的第二个域的头部添加了访问控制允许的来源:*。

2 个解决方案

#1


1  

An HTTP redirect page is treated as any other HTTP page in that it also needs the access control headers. If your redirect page does not have them, the browser will never get around to checking if the page being redirected to has the proper permissions.

HTTP重定向页面被视为任何其他HTTP页面,因为它还需要访问控制头。如果重定向页面没有它们,浏览器将永远不会检查被重定向到的页面是否具有适当的权限。

Along with the Location header on the redirect page, also add the Access-Control-Allow-Origin header and its related constituents (i.e. Access-Control-Allow-Methods etc.)

除了重定向页面上的位置标头之外,还要添加访问控制允许的源标头及其相关成分(例如,访问控制允许的方法等)。

#2


0  

The only way to get a cross-domain ajax call is to use jsonp.

获得跨域ajax调用的唯一方法是使用jsonp。

In jQuery, set your .ajax() dataType to 'jsonp'. See here: http://api.jquery.com/jQuery.ajax/

在jQuery中,将.ajax()数据类型设置为“jsonp”。在这里看到的:http://api.jquery.com/jQuery.ajax/

It still may not work, if the server being redirected to is not capable of a jsonp response. The difference between a json response and a jsonp response is that a json response is a pure json string, while a jsonp response is the code that calls a function passing in a json string.

如果被重定向到的服务器无法进行jsonp响应,那么它仍然可能无法工作。json响应和jsonp响应的区别在于json响应是纯json字符串,而jsonp响应是调用传入json字符串的函数的代码。

A not-too-shabby tutorial: http://remysharp.com/2007/10/08/what-is-jsonp/

还不算太糟教程:http://remysharp.com/2007/10/08/what-is-jsonp/

A good discussion: Can anyone explain what JSONP is, in layman terms?

一个很好的讨论:有人能解释一下JSONP是什么吗?

#1


1  

An HTTP redirect page is treated as any other HTTP page in that it also needs the access control headers. If your redirect page does not have them, the browser will never get around to checking if the page being redirected to has the proper permissions.

HTTP重定向页面被视为任何其他HTTP页面,因为它还需要访问控制头。如果重定向页面没有它们,浏览器将永远不会检查被重定向到的页面是否具有适当的权限。

Along with the Location header on the redirect page, also add the Access-Control-Allow-Origin header and its related constituents (i.e. Access-Control-Allow-Methods etc.)

除了重定向页面上的位置标头之外,还要添加访问控制允许的源标头及其相关成分(例如,访问控制允许的方法等)。

#2


0  

The only way to get a cross-domain ajax call is to use jsonp.

获得跨域ajax调用的唯一方法是使用jsonp。

In jQuery, set your .ajax() dataType to 'jsonp'. See here: http://api.jquery.com/jQuery.ajax/

在jQuery中,将.ajax()数据类型设置为“jsonp”。在这里看到的:http://api.jquery.com/jQuery.ajax/

It still may not work, if the server being redirected to is not capable of a jsonp response. The difference between a json response and a jsonp response is that a json response is a pure json string, while a jsonp response is the code that calls a function passing in a json string.

如果被重定向到的服务器无法进行jsonp响应,那么它仍然可能无法工作。json响应和jsonp响应的区别在于json响应是纯json字符串,而jsonp响应是调用传入json字符串的函数的代码。

A not-too-shabby tutorial: http://remysharp.com/2007/10/08/what-is-jsonp/

还不算太糟教程:http://remysharp.com/2007/10/08/what-is-jsonp/

A good discussion: Can anyone explain what JSONP is, in layman terms?

一个很好的讨论:有人能解释一下JSONP是什么吗?