jQuery AJAX请求302重定向-有哪些回调可用? [英] jQuery AJAX Request 302 Redirect - What callbacks are available?
问题描述
我正在使用使用jQuery 1.2.6的旧系统.我正在通过jQuery.ajax
函数发送AJAX请求.它所命中的URL发送了302 HTTP重定向响应,最终以200 HTTP OK响应结束.我已经注册了success
和complete
回调,但是都没有被调用.
I'm working with an older system that is using jQuery 1.2.6. I am sending an AJAX Request via the jQuery.ajax
function. The URL that it is hitting is sending a 302 HTTP Redirect response and eventually ends up with a 200 HTTP OK response. I have registered both a success
and a complete
callback, however neither of them are called.
我的问题是:重定向发生后,是否可以/将要调用任何回调?
My question is: Are there any callbacks that can/will be called after a redirect occurs?
jQuery.ajax({
type: "GET",
url: url,
data: null,
dataType: "json",
async: false,
success: function(data, textStatus) {
alert("SUCCESS CALLED: " + textStatus);
},
complete: function(xhr, status) {
alert("COMPLETE CALLED");
}
});
注意::响应是HTML,而不是JSON,但是将dataType
更改为html
会更改请求.它发送一个OPTIONS
请求而不是一个GET
请求,并且它也不再重定向.我需要进行这些重定向.
NOTE: The response is HTML, not JSON, however changing the dataType
to html
alters the request. It sends an OPTIONS
request instead of a GET
request, and it also no longer redirects. I need these redirects to occur.
推荐答案
根据评论:
将
dataType
更改为html
会更改请求.它发送OPTIONS
请求而不是GET
请求,并且也不再重定向.
Changing the
dataType
tohtml
alters the request. It sends anOPTIONS
request instead of aGET
request, and it also no longer redirects.
当您发送跨域请求时,就会发生这种情况.也就是说,url
所指向的域与提供该脚本的域不同.这即违反了相同来源政策.没有访问服务器代码的权限,也确认它在其他域上运行. jQuery发送的HTTP OPTIONS
请求应允许服务器返回带有 Allow
标头的响应带有允许在特定URL上使用的请求方法列表,以便jQuery随后可以继续进行.显然这没有发生.
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.
如果服务器确实完全不受您的控制,则需要在服务器上创建一个代理,该代理连接所需的URL并将其响应流回.然后更改jQuery url
使其指向代理URL.目前尚不清楚您使用的是哪种编程语言,但是您的问题历史记录表明您熟悉Java,在这种情况下,您可以将简单的servlet与 Apache HttpComponents客户端.
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.
这篇关于jQuery AJAX请求302重定向-有哪些回调可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!