vincent15000's avatar

Fetch : TypeError: NetworkError when attempting to fetch resource.

Hello,

I have this problem.

If I run this code step by step with the debuger, all works fine.

But if I run this code normally, I get this error.

TypeError: NetworkError when attempting to fetch resource.

I think that it can be a problem with synchon / asynchron function, but I already have a promise.

const apiUrl = 'http://localhost:5678/api/students';
fetch(apiUrl, {
    method: 'POST',
    headers: {
        'Authorization': `Bearer ${token}`
    },
    body: data,
}).then((res) => {
    if (res.ok) {
        console.log('success');
    } else {
        throw new Error('Error status code: ' + res.status);
    }
}).catch((error) => {
    console.log(error);
});

What could be the source of the problem ?

Thanks for your help.

V

0 likes
5 replies
vincent15000's avatar

@m7vm7v I also had this idea, but if it were a CORS problem, the request should return an explicit error message saying that the request was blocked, no ?

And if it were a CORS problem, it would not work even step by step.

Snapey's avatar

What do you see in the debug tools network tab

1 like
vincent15000's avatar

@Snapey That's the problem, I don't see anything but the line with the POST request to the server (no information inside the different tabs, no status code, no response, ...).

Unless the code is executed step by step, then all is ok, I can see the payload and all the other informations.

Amalphi's avatar

I faced the same issue. I fixed it by replacing the submit button in my form by a normal button .

Please or to participate in this conversation.