I am developing an Apache Cordova app and use your polyfill because iOS does not support the fetch API at the moment. Unfortunately this polyfill does not work when the server throws an http 401.
For example I make a fetch POST request like this one
fetch(address, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
'my': 'body'
}),
})
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
})
On Android I can see the thrown error, on iOS nothing happens. The code block in .then is never called. It seems that fetch swallowed everything and does not resolve the promise.
It might be that in a case of 401, this browser rejects the promise instead of resolves it. That would explain why the then block hasn't been called. Try this instead:
.then(response => alert("done"), error => alert("oh no!"))
The error handler might be invoked here.
No, it's not working. The AJAX request is never returned. In Safari developer tools you can see that the request is never ending. Please reopen this issue.
Because we don't have experience with Apache Cordova, we can only provide suggestions to help debug problems. Usage questions are best suited for StackOverflow because you'll find people with Cordova experience there.
If you have a reproducible test case using the Safari browser on iOS, without Cordova in the mix, we can take a closer look.
One other thing to try to debug this is using a catch handler. There are two ways to catch errors:
// A second function argument to `then`
fetch(url).then((response => console.log(response)), (error => console.log(error)))
// A `catch` handler on the Promise
fetch(url).then(response => console.log(response)).catch(error => console.log(error))
Hmm that's strange. I created another hello world App and there it's working as expected. Also I created a sample web page that you can open in your browser: it works. Strange. I will look deeper into that in the next few days. As I said the AJAX request is hanging and does not return (I can see that in the developer tools in Safar. There is a spinning wheel that never ends spinning) :confused:
For the record: it's a general iOS :beetle: in AJAX requests. It has nothing to do with the fetch polyfill. I found this StackOverflow issue.
any news in this? I experiencing same issues.
Hi @zooldk,
as I mentioned before: it is not a bug in this polyfill. It's a general bug on iOS. You can't handle http 401 errors from JavaScript on iOS. The only thing you can do is to remove the WWW-Authenticate server response header if you have access to the server implementation. Then it should work, but that is a little bit hacky.
Most helpful comment
No, it's not working. The AJAX request is never returned. In Safari developer tools you can see that the request is never ending. Please reopen this issue.