Hello
I know that free hosting on infinity free does not support API hosting - But what if the API in my application only is for internal use?
I have some JavaScript and HTML inside my project, which sends a fetch request to its own URL, but with a POST request it gives an error saying the problem is something with CORS? But it works when I just want to GET or DELETE something from my internal JavaScript. Can it not work with a POST?
You indeed cannot use our hosting to host an API only, but you can host a PHP backend application and Javascript frontend that talks to the backend over a HTTP API.
Our hosting only supports GET and POST HTTP methods. Other HTTP methods, like PUT, PATCH and DELETE are blocked.
But you seem to have a CORS error on a HTTP POST request. POST requests are supported, and CORS errors only happen when you try to do things across domains.
Can you share the URL to where your HTML and Javascript pages live, and tell us how we can reproduce this issue?
The code isn’t usable right away, but it does help! And I think I found the issue.
The problem is that your website is using http://, but you are making the fetch request to https://. However, when you try to visit that URL directly, you will see that your website does not have a valid SSL certificate.
Firefox pointed me to this article for more information about the error:
And this article specifically lists:
Trying to access an https resource that has an invalid certificate will cause this error.
So that’s why you get the error.
To fix this, you can do two things:
As a workaround, you can change https:// to http:// in the fetch function, or just change it to //digital-media-api.infinityfreeapp.com/api/${endpoint} so it will use https:// if you are viewing the page with https:// and http:// if you are viewing it with http://.
If you want your website to support HTTPS, you’ll need to install a valid SSL certificate. We provide those for free, but you will have to go through a setup procedure for it: How to get Free SSL (HTTPS) on InfinityFree
Wow, thank you! I did not see that mistake
It works now with POST! Thanks for telling me that DELETE and PUT doesn’t work, so I don’t need to fight with that anymore.