Any cross-domain API call needs CORS enabled on server hosting the API for success calls.
This can become a problem if you are not the owner of the server and you just need to send some data to the API without expecting any response data.
If you want to know more about CORS and its security, follow my other blog https://medium.com/@kurohashi/cors-in-depth-c8daa2925248
We at fibotalk needed to know how much time a user is spending on a page to know his/her journey in a session. For that purpose, we needed to capture the page closing event when the browser tab or window is closed by the user.
Worry not!, JS developers have not abandoned you. There’s a very simple solution for you:
window.navigator.sendBeacon(“www.sample.com/api/”, JSON.stringify(data));
This will send a POST request to “www.sample.com/api/” with Body of the request as stringified data. No CORS issue will come up in this case.
Please, note that JSON directly is not allowed in the data field of the parameter.
That’s it. See!, how easy it is.
See us at https://www.fibotalk.com/