是否可以通过CORS策略模拟XMLhttpRequest块?

时间:2019-08-09 14:46:19

标签: angular cors

我需要处理此错误

Access to XMLHttpRequest at 
'my http request url' (redirected from 'my web request url')
 from origin 'web origin'
 has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

在我的本地环境中。 它仅在我不在本地主机中时发生,并且我在会话过期后尝试向后端发出api请求。 我不处理会话管理系统,也不需要修复错误 我只需要模拟它来处理错误并通过模态向用户显示消息,但是此错误与正常的HttpErrorResponse不同,我无法正确获取消息,也无法在该环境中调试。

不可能调试

到目前为止我的错误处理

protected handleError(error: any) {
    let errMsg = "";
    if (error.status == 0) {
      window.location.reload();
    } else {
      if (error.status < 100 || error.status > 599) {
        errMsg = error.message;
      } else {
        if (error.error.message != undefined) {
          errMsg = error.error.message;
        } else {
          if (error.status) {
            errMsg = error.status + " - " + error.statusText;
          } else {
            errMsg = "Server Error.";
          }
        }
      }
    }
    return throwError(errMsg);

1 个答案:

答案 0 :(得分:1)

我认为您需要拦截器HTTP错误。

这里有一个示例Angular authentication using http

要使用模式,我建议使用CDK覆盖modal pop-up with CDK,下一步将服务放在拦截器构造函数中……然后尝试!