【问题标题】:Firefox gives Network Error whereas other browsers work just fine (ReactJS App)Firefox 出现网络错误,而其他浏览器工作正常(ReactJS 应用程序)
【发布时间】:2018-09-25 23:20:40
【问题描述】:

我使用 React.js 制作了一个 Web 应用程序,并通过 Axios 调用后端。

但是,Firefox 在等待服务器响应时不断给我一个网络错误。其他浏览器,如 Chrome、Safari、Opera 都可以正常工作。

请求代码:

export function postLoginDetails(username, password, token) {
const url = ENV.host + "/login";

return axios({
    method: 'post',
    url: url,
    withCredentials: true,
    headers: {
        "X-CSRFToken": token
    },
    data: {
        username: username,
        password: password,
    },
    responseType: 'json'
   }).catch(e => console.log(e))
}

这是发生错误的代码:res is undefined。

postLoginDetails(username, password, this.token).then((res) => {
                if (res.data.success) {
                    this.isAuthenticated.set(true)
                    cb();
                } else {
                    this.error.set(true);
                    this.errorText = observable('Fehler aufgetreten');
                    this.name = observable('');
                }
                this.loginLoading.set(false);
            })

Heres the error in Firefox

【问题讨论】:

  • @Liam 它是一个节点 js express 服务器
  • 嗯,您看到的是网络错误还是只是在控制台中?您是否只看到飞行前请求“OPTION”?
  • 我猜这是因为您支持的服务器没有响应带有标头的飞行前请求以允许客户端服务器发送 POST 请求
  • 我知道这是一年多以前的事了,但你有想过这个吗?这里有完全相同的问题。
  • @minus.273 如果我没记错的话,在触发请求的按钮上设置type="button" 会导致它正常工作,我不必切换到ajax jquery,虽然我没有'不完全理解为什么会这样。

标签: javascript reactjs firefox browser axios


【解决方案1】:

对于遇到此问题的其他人,我无法谈论飞行前选项,但我知道 firefox 正在告诉 axios 终止请求。如果您查看处理错误的 axios 部分,您会看到注释:

// Handle browser request cancellation (as opposed to a manual cancellation)

所以这是某种浏览器的怪癖。我没有使用按钮触发请求,而是使用锚标记<a>。当我将其更改为 <span> 时,它开始工作。为什么,火狐?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 2023-04-04
    • 2015-05-26
    • 1970-01-01
    • 2015-06-22
    • 2011-02-10
    • 2020-11-27
    • 2014-01-14
    相关资源
    最近更新 更多