“尝试获取资源时出现网络错误."仅在火狐上 [英] "NetworkError when attempting to fetch resource." only on Firefox

查看:30
本文介绍了“尝试获取资源时出现网络错误."仅在火狐上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 fetch API 从我的前端发出 POST 请求.但是当我在 Firefox 中尝试时,它不起作用.在 Chrome 中工作正常.

I'm doing a POST request from my frontend using fetch API. But when I tried in Firefox, it doesn't work. In Chrome works fine.

这就是我想要做的.

const handleSubmit = async event => {
        try {
            await fetch(`https://api.example.net/api/route?slug=example`, {
                method: 'post',
                headers: {
                    'Content-Type': 'application/json',
                    'x-api-key': /* API KEY*/
                },
                body: JSON.stringify({
                    name,
                    email
                })
            })
                .then(response => console.log(response))
                .catch(err => console.log(err));
        } catch (error) {
            console.log(error);
        }
  };

推荐答案

所以,伙计们,这就是解决方案.

So, guys, here's the solution.

问题是刷新表单的时间,是在发送之前刷新.为了解决这个问题,设置在响应时刷新表单,就完成了!

The problem was the time for refreshing the form, is refreshing before send it. To solve this, set to refresh the form on response, and is done!

const handleSubmit = async event => {
        event.preventDefault();
        try {
            await fetch(`https://api.example.net/api/route?slug=example`, {
                method: 'post',
                headers: {
                    'Content-Type': 'application/json',
                    'x-api-key': /* API KEY*/
                },
                body: JSON.stringify({
                    name,
                    email
                })
            })
                .then(response => location.reload())
                .catch(err => console.log(err));
        } catch (error) {
            console.log(error);
        }
  };

这篇关于“尝试获取资源时出现网络错误."仅在火狐上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆