I have a dashboard that works by sending APIs to other parts of my server. It's a simple html form that is processed with JavaScript to send a fetch request, and the sent request is handled by my flask backend to do the task with the provided data.
The issue is that when the request is sent on desktop, it works properly. When I am sending it in iPhone using google chrome, the request is not sent properly. Instead of a 'POST' request, it is sending an 'OPTIONS' request.
Server logs show: "OPTIONS /api/message HTTP/1.1" vs desktop version of "POST /api/message HTTP/1.1"
Here is the JavaScript code:
const options = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify( params )
};
fetch('https://myUrl.com/api/message', options )
.then( response => response.text() )
.then(response => {
if (response == 'SUCCESS') {
console.log(response)
sessionStorage.setItem("success", "true");
window.location.reload()
} else {
sessionStorage.setItem("error", response)
window.location.reload()
}
})
CodePudding user response:
My guess is that the browser sends a preflight request.
Different behaviour on desktop & phone might be explained by some phone-only optimizations implemented by browser's vendor.
CodePudding user response:
It is sending a preflight request. I believe this is because I was using the HTTP instead of HTTPS and the request sends to HTTPS.