[CLOSED] CORS error using the Locale API from a browser: CORS Allow Origin Not Matching Origin
- 0
- Resolved
- 2
- Self Hosted
- Web
When I call the GET /v1/locale API with JS in a browser the GET request fails with CORS Allow Origin Not Matching Origin.
Requests to GET /v1/databases/... work fine and return the correct access-control-allow-origin header (domain.com), but the /v1/locale returns the Appwrite url only (api.domain.com).
The OPTION request for the locale also succeeds with the correct header.
I have added the domain as a platform in the console.
Are you including your project ID in the request header?
Yes, it's sent in the X-Appwrite-Project header. The Databases API and Account API work but not the Locale API
Can you share the code with me?
Only the part where you're handling this
And have you added your domain in the platform?
Here's the code: https://pastebin.com/TgLCanTc
And yes it's added as a Web platform in the console
Response headers: Locale API: https://pastebin.com/rkzUxd9x Databases API: https://pastebin.com/YTewuSB3
The response headers for the OPTIONS request are domain.com for both, it's just the GET request that is different
You have an issue in your code (as per what I know about initializing clients) - In this part:
let client = null;
let databases = new Databases(client);
let locale = new Locale(client);
you are constructing the DB & Locale with client as null.
Then again in this:
export function initClient(config, sessionToken = null) {
client = new Client()
.setEndpoint(config.public.appwriteEndpoint)
.setProject(config.public.appwriteProjectId);
if (sessionToken != null) {
client.setSession(sessionToken);
}
databases = new Databases(client);
locale = new Locale(client);
}
you are re-defining them. But what's going wrong is that when the part let client... is read, it supposes you've your client undefined. Which causes issues. So please fix your code like this and check whether it works:
import { Client, Databases, Locale } from "appwrite";
let client = null;
let databases = null;
let locale = null;
export function initClient(config, sessionToken = null) {
client = new Client()
.setEndpoint(config.public.appwriteEndpoint)
.setProject(config.public.appwriteProjectId);
if (sessionToken) client.setSession(sessionToken);
databases = new Databases(client);
locale = new Locale(client);
}
export function getLocale() {
return locale;
}
export function getDatabases() {
return databases;
}
I changed it to what you wrote but still get the CORS error
Ok. Have checked all your endpoint urls are correct?
And one more thing, can you please also share the Console & Network output?
Yes, the endpoint is only set once when the initClient function is called the first time (and all other Appwrite calls succeed, just not the Locale one) π
I deleted the platform and added it back and restarted the appwrite containers and now it suddenly works without issues
So no errors now?
No, the locale call returns {"ip":"172.18.0.1","countryCode":"--","country":"Unknown","continentCode":"--","continent":"Unknown","eu":false,"currency":""}, just need to forward the IP from Nginx π
[CLOSED] CORS error using the Locale API from a browser: CORS Allow Origin Not Matching Origin
Recommended threads
- education plan not activated
Hi I have an edu id 13103046@iubat.edu but when I am trying to claim my plan and trying to logging with github where education student plan active. the appwrite...
- I'm getting an error on the console "j?....
On my self hosted instance version 1.8.1 the console is giving me this error when trying to view the rows for a table I recently created. My application is read...
- 500 simultaneous OAuth logins from the s...
Hi, I'd like to ask about rate limiting around Google OAuth login on Appwrite Cloud. **OVERVIEW** Service type: A PWA (web app) for members of a university clu...