How can I spot a 302 response in Sencha Touch Ajax Request

血红的双手。 提交于 2019-12-25 00:52:48

问题


I am making an Ajax.request to a backend I don't control.

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

    });

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.

Any ideas?

Regards.


回答1:


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 );
        }
    });
}

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.



来源:https://stackoverflow.com/questions/24816478/how-can-i-spot-a-302-response-in-sencha-touch-ajax-request

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