fetch('http://asfklajsdflkajsdfkj.com/test', {
method: 'POST',
headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"},
body: "InpNumber=" + InpNumber.value
}).then(function(response) {
console.log(response);
var status = response.status; // Get the HTTP status code
var response_ok = response.ok; // Is response.status in the 200-range?
return response.json(); // This returns a promise
}).then(function(response) {
if(response.result == 'success'){
ActivationID = response.data.validation;
setState(3, null);
} else {
setState(2, response.error);
}
}).catch(function(err) {
console.log(err);
setState(2, "An error has occurred");
});
I have put in a random URL in the fetch parameters as shown to simulate what it would be like if there was a server down or not found. For some reason fetch doesn’t return any response when there is a 404 error or throw an error that is caught. Any ideas on how to catch 404 error?
A random generated URL is a very bad test-case since you never know what you will hit. In the case above you try to target a domain that does not exists. So the server can not reply with any errors at all. So you don’t get any answer. 404 is a response you get when a client has entered an URL that the server does not understand, hence you need to have communication with the server first.
After testing I can see that we are not throwing any exception. That could be because the timeout is very long, but I’m not sure. I’ve raised an internal issue to investigate.
I can’t say when FormData will be available because we have a lot of other high pri tickets on HTTP that needs to be done first. But we do have it on the list.
Fetch cancel: Yes, that is indeed something we want. But as you can see from the whatwg issue 27 this is a very complex problem. We are following the effort here that we hope will end up as a general solution we can depend on.
If you need something like timeout or cancel, you could use setTimeout or use XMLHttpRequest.abort().