我正在使用extjs Ext.Ajax.request从我的应用程序调用REST API。我正在使用oauth来验证REST API。如果调用ext.ajax.request时令牌已过期,则需要添加逻辑以刷新令牌,如果令牌已过期,则需要刷新逻辑,然后刷新令牌并使用相同的机制再次调用API。如何在不更改每个ext.ajax.request调用的情况下实现呢?
您可以监视Ext.data.connection类的requestexception事件。使用成功处理程序,您可以在成功获取访问令牌后再次运行失败的请求,例如如果无法刷新令牌,请注销。
Ext.util.Observable.observe(Ext.data.Connection, {
requestexception: function(conn, response, options) {
if (response && (response.status === 401 && response.statusText === "Unauthorized")) {
MyLoginService.refreshToken(function() {
//success callback
conn.request(options);
}, function() {
//failure callback
MyLoginService.logout();
});
}
},
}, this);