jQuery AJAX Request 302 Redirect - What callbacks are available?

两盒软妹~` 提交于 2019-12-01 08:57:48

As per the comments:

Changing the dataType to html alters the request. It sends an OPTIONS request instead of a GET request, and it also no longer redirects.

This will happen when you send a crossdomain request. I.e., the url does not point to the same domain as where this script is been served from. This namely violates the Same Origin Policy. Not having access to the server code also confirms that it runs on a different domain. The HTTP OPTIONS request sent by jQuery should let the server return a response with an Allow header with a list of request methods which are allowed to be used on the particular URL so that jQuery could then continue accordingly. This is apparently not happening.

If the server is really completely out your control, you'd need to create a proxy on your server which connects the desired URL and streams its response back. Then alter the jQuery url to point to the proxy URL instead. It's unclear what programming language you're using, but your question history suggests that you're familiar with Java, in that case you can use a simple servlet with java.net.URLConnection or Apache HttpComponents Client for this.

Use jquery ajax option

dataType: 'jsonp'

to perform the redirect for cross-domain requests. (Was tested in FF/Chrome browsers)

But this resolution is useful only if you are not handle the response or if the cross-domain-url supports jsonp approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!