问题描述
我正在向不受控制的后端发出Ajax.request.
I am making an Ajax.request to a backend I don't control.
此请求有时会将我重定向到登录页面,我的response.status是200而不是302.到目前为止,我已经尝试过:
This request sometimes redirects me to the login page, and my response.status is 200 instead of 302. So far I have tried this:
Ext.Ajax.on("requestexception", function(conn, response, options, eOpts){
console.log(conn);
console.log(response);
console.log(options);
console.log(eOpts);
});
Ext.Ajax.request({
url : 'someUrl'
params : params
});
很明显,这种重定向不是我期望的,因此我需要确定304发生的时间.
Obviously this redirection is not what I expected so I need to spot when a 304 happened.
大多数情况下都可以解决.
There most be some kind of work around.
有什么想法吗?
关于.
推荐答案
据我所知,http重定向完全由浏览器处理.因此,如果您无权访问后端,则无法检测重定向.
As far as I know http redirects are handled entirely by the browser. So there is no way to detect a redirect if you don't have access to the backend.
当您重定向到登录页面时,您的会话似乎已过期,需要再次进行身份验证.
When you are redirected to the login page it seems that your session is expired and you need to authenticate again.
您可以创建一个函数,以在实际响应中检测到登录页面后立即发送登录信息.
You could create a function that sends the login information as soon as the login page is detected in the actual response.
sendLogin: function ( params, successCallback, failureCallback, scope ) {
Ext.Ajax.request({
url: "loginurl",
params: params,
success: function ( response, options ) {
successCallback.call( scope || this, response, options );
},
failure: function ( response, options ) {
failureCallback.call( scope || this, response, options );
}
});
}
doRequest: function ( params, successCalback, failureCallback, scope ) {
var me = this;
Ext.Ajax.request({
url: "someurl",
success: function ( response, options ) {
if ( isLoginPage( response ) ) {
this.sendLogin(
loginParams,
function ( successResponse, successOptions ) {
me.doRequest( params, successCallback, failureCallback, scope );
},
function ( failureResponse, failureOptions ) {
failureCallback.call( scope || this, failureResponse, failureOptions );
},
me
);
} else {
successCallback.call( scope || this, response, options );
}
},
failure: function ( response, options ) {
failureCallback.call ( scope || this, response, options );
}
});
}
使用doRequset
发送您的实际请求.成功案例检查响应是否为登录页面.如果是这样,它将发送登录请求.登录请求成功后,将再次使用其参数调用doRequest函数.
Use the doRequset
to send your actual request. The success case checks if the response is the login page. If so, it sends the login request. When the login request is successful the doRequest function will be call again with its parameters.
这篇关于如何在Sencha Touch Ajax Request中发现302响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!