【问题标题】:scripts requested via systemjs fail with 401 Unauthorized通过 systemjs 请求的脚本失败并显示 401 Unauthorized
【发布时间】:2016-03-10 15:05:17
【问题描述】:

我正在为 Team Foundation Server 2015 构建一个 hub extension

在我切换到使用systemjs 和 angular2 之前,我的 Hub HTML 页面运行良好,此时来自 systemjs 的任何脚本请求似乎都被 401 未经授权的拒绝。

我密切关注Angular2 Quickstart 指南,无需身份验证即可在本地正常运行。

但是一旦我将我的应用程序上传到 TFS,systemjs 请求的任何脚本都会被拒绝,如下所示:

Error: XHR error (401 Unauthorized) loading http://tfs-server:8080/tfs/_apis/public/gallery/publisher/my-publisher/extension/source-extensions/1.1.2/assetbyname/scripts/main.js(…)

Zone.run @ angular2-polyfills.js:1243
zoneBoundFn @ angular2-polyfills.js:1220
lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:468
lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:480
lib$es6$promise$$internal$$publish @ angular2-polyfills.js:451
lib$es6$promise$$internal$$publishRejection @ angular2-polyfills.js:401
(anonymous function) @ angular2-polyfills.js:123
Zone.run @ angular2-polyfills.js:1243
zoneBoundFn @ angular2-polyfills.js:1220
lib$es6$promise$asap$$flush @ angular2-polyfills.js:262

如果我通过脚本标签加载我的脚本,则不存在授权问题。但是当我像这样通过 systemjs 执行此操作时,它会因 401 错误而失败:

<script>
  System.config({
    packages: {
      scripts: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });
  System.import('scripts/main.js')
        .then(null, console.error.bind(console));
</script>

我的问题是:当使用 systemjs 加载脚本而不是直接加载它们时,我是否需要做一些额外的事情来避免 401 未经授权?

【问题讨论】:

  • 这听起来像是你的服务器中的配置,而不是 angular 2
  • 请记住,脚本在没有 systemjs 的情况下可以正常加载,但在 systemjs 加载时会收到 401 未授权。这应该使它成为客户端问题,而不是服务器配置问题。

标签: javascript tfs angular systemjs


【解决方案1】:

SystemJS 将使用 XHR 加载代码,这可能是这里的不同之处。

如果你想让 SystemJS 使用

  System.config({
    packages: {
      scripts: {
        format: 'register',
        defaultExtension: 'js',
        scriptLoad: true
      }
    }
  });

如果还是这样,那肯定是服务器配置了。

【讨论】:

  • 这让我更进一步......我添加了 scriptLoad: true 然后脚本成功加载而没有出现 401 错误。所以这确实解决了 401 身份验证问题,所以谢谢你!但我立即收到错误Uncaught TypeError: Unexpected anonymous System.register call
  • 请参阅here 了解继续对话和解决方案。
  • 我有点晚了,但我认为用脚本标签膨胀 DOM 并不是一个理想的解决方案。我将非常感谢使默认 SystemJS XHR 模式工作的解决方案。
猜你喜欢
  • 1970-01-01
  • 2021-06-14
  • 2021-12-14
  • 1970-01-01
  • 2022-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多