【问题标题】:axios.post works in chrome but not in firefoxaxios.post 适用于 chrome 但不适用于 Firefox
【发布时间】:2018-09-08 11:56:25
【问题描述】:

大家好!

我有一个用 vuejs 制作的 SPA,也使用了 webpack。为了进行 http 调用,我正在使用 axios,但是发生了一些非常奇怪的事情......

当我在 CHROME 上使用 spa 时一切正常,但是任何其他浏览器(safari、firefox、iosSafari)都会给我以下错误:

Error: Network Error
Stack trace:
createError@webpack-internal:///34:16:15
handleError@webpack-internal:///33:88:14

Error shown in console (more details)

进行此调用的代码是:

  const rep = localStorage.getItem('rep')
  const token = localStorage.getItem('token')
  const config = {
    headers: {
      'Content-Type': 'application/json',
      'x-access-token': token
    }
  }
  axios.post('localhost:1234/statusproducao', { representante: rep }, config)
    .then((response) => {
      Loading.hide()
      this.statusPinos = response.data.statusProducao
    })
    .catch((error) => {
      Loading.hide()
      console.log(error)
    })

查看 devtools --> network,我可以看到浏览器没有发出 post 请求,而之前显示的错误来自 axios.post 的 .catch(error)

const rep 和 token 都是字符串。

我错过了什么?

【问题讨论】:

  • 这与CORS有关吗?您的 SPA 在哪个端口上运行?
  • api 在 localhost:1234 上运行,并且正确配置了 cors 我认为这是我正在使用的方法中的一些东西,因为我可以在应用程序中登录 ...
  • @varunvs 谢谢,这实际上是一个 cors 问题。

标签: javascript firefox safari axios


【解决方案1】:

我在我的 cors 上添加了“x-access-token”标头,在路由中,我也需要这个:

'x-access-token':令牌

  server.opts(/\.*/, function (req, res, next) {
    res.send(200)
    next()
  })

【讨论】: