在 Vue.js 中使用 JavaScript 处理 GET 301/302 响应

Handling GET 301/302 responses with JavaScript in Vue.js

我需要用 JavaScript 打印一些 301/302 响应 headers:我看到很多关于 Angular 的答案,但我正在使用 Vue.js 来获取结果。好吧,这并不重要,因为它主要是 JavaScript 问题……我的意思是,我不仅对 Vue.js- 感兴趣 [=27] =] 解决方案,它可以而且应该在任何地方都有效。作为一名 front-end 开发人员,我无法手动创建一个方案,在该方案中,我的项目 returns 在其自己的服务器中进行重定向并找到一个没有 CORS 限制的随机远程页面很难。更清楚地说,我需要在重定向之前获取 status 代码和 301/302 响应的 statusText(以及新页面的 Location),然后是完整的 headers 的重定向页面。现在我只能检索最后这些,因为 vue-resource 创建的 the default this.$http.get(); request 似乎没有将第一个存储在 object 中。我在这里要问的是,是否有一种方法可以将重定向响应也存储在一个变量中,然后同时显示两者。我不知道切换到 axios 是否更可取——因为我是 Vue.js 的新手。我的组件方法如下:

getRequest: function() {
  this.$http.get(this.url)
    .then(function(response) {
      if (response.ok) {
        if (response.status === 301 || response.status === 302) {
          // show the redirecting response
        }
      // show the redirected response
      }
    }

编辑:the sources for this project 在 GitHub 上;当您发送 GET 请求并且响应为 301/302 时,您应该能够看到三列而不是两列,其中第二列显示重定向响应的详细信息 headers.

您无法处理 301 或 302 状态,因为它们在

if(response.ok)

块,这意味着状态为 200。试试这个:

   getRequest: function() {
  this.$http.get(this.url)
    .then(function(response) {
      if (response.ok) {
      // show the redirected response
      }
      if (response.status === 301 || response.status === 302) {
          // show the redirecting response
      }
    }