Hi everyone,
I recently encountered a CORS issue with a custom Google Autocomplete API in a web app, and I couldn't find a clear solution online. So, I decided to share what worked for me.
Here's what I tried before finding the fix:
Making the API private
Modifying the cloud function based on suggestions from ChatGPT and Claude in at least five different ways
https://community.flutterflow.io/ask-the-community/post/google-places-api-cors-1AlrrxtaK4SRYef
https://community.flutterflow.io/ask-the-community/post/google-places-api-cors-1AlrrxtaK4SRYef
But none of these helped.The Solution
Then I found a really simple workaround: https://github.com/Rob--W/cors-anywhere/
I just added Content-Type: application/json as header to the api
In the advanced settings I added https://cors-anywhere.herokuapp.com/ to the Proxy prefix url
Then Added a custom header to the web app:
<script>
(function() {
var cors_api_host = 'cors-anywhere.herokuapp.com';
var cors_api_url = 'https://' + cors_api_host + '/';
var slice = [].slice;
var origin = window.location.protocol + '//' + window.location.host;
var open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
var args = slice.call(arguments);
var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
if (targetOrigin && targetOrigin[0].toLowerCase().includes('maps.googleapis.com')) {
args[1] = cors_api_url + args[1];
}
return open.apply(this, args);
};
})();
</script>
And that's it.
Hope this helps anyone facing a similar issue!