2

I am working with HCP Portal SAPUI5 apps. I need to check the session before every data call is made to the backend so I can redirect the user back to the logon page.

In the HANA Cloud documentation, the below code is provided:

jQuery(document).ajaxComplete(function(e, jqXHR) {
  if (jqXHR.getResponseHeader("com.sap.cloud.security.login")) {
    alert("Session is expired, page shall be reloaded.");
    jQuery.sap.delayedCall(0, this, function() {
      location.reload(true);
    });
  }
});

But the above code only works for Ajax calls. I am not sure if the same works for odata as well. We want to redirect the user in every scenario after session expiry. Is there a direct method to achieve it both for data calls and Ajax calls?

4

1 回答 1

1

您可以在成功回调函数中检查 HTTP 响应标头的值"com.sap.cloud.security.login"

  sap.ui.getCore().getModel().read("/SOME_ENTITYSet", {

     success: function(odata, response) {
       if (response.headers["com.sap.cloud.security.login"] === "login-request") {
          // Timeout handling
       } else {  
          // Process data in argument odata  
       }
     },

     error: function(error) {
       if (response.headers["com.sap.cloud.security.login"] === "login-request") {
          // Timeout handling
       } else {  
          // Show error message (for non-timeout errors)
       }         
     }
  });  

如果已经看到在超时时调用成功回调函数的情况;但是我也看到了调用错误回调函数的情况;因此我在这两种情况下都检查超时。

超时处理可能是一个对话框,告诉用户会话已超时并询问他是否要重新启动应用程序。

于 2017-06-12T11:34:05.957 回答