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

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

问题描述

我正在使用 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);
        }
  };

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

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