jQuery ajax call to domain that redirects to another domain (isn't followed)
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.
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?
I already added access-control-allow-origin: *
to the header of the second domain that is being redirected to.
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.
Along with the Location
header on the redirect page, also add the Access-Control-Allow-Origin
header and its related constituents (ie Access-Control-Allow-Methods
etc.)
The only way to get a cross-domain ajax call is to use jsonp.
In jQuery, set your .ajax()
dataType to 'jsonp'. See here: 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.
A not-too-shabby tutorial: http://remysharp.com/2007/10/08/what-is-jsonp/
A good discussion: Can anyone explain what JSONP is, in layman terms?
链接地址: http://www.djcxy.com/p/47508.html上一篇: 从JavaScript代码使用jsonp