Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...