Skip to content
Advertisement

“NetworkError when attempting to fetch resource.” only on Firefox

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.

Here’s what I’m trying to do.

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);
        }
  };

Advertisement

Answer

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);
        }
  };
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement