CORS Problem with custom api

Hello dear colleagues
I have created this api.py file => my-url/api/method/erpnext.api.test and it is working fine with postman but gives CORS error whenever I try to use it in my web application

Sample code:

        import frappe
        @frappe.whitelist(allow_guest=True)
        def get_invoices():
            return frappe.db.sql(f"""select * from `tabSales Invoice` where 1;""", as_dict=True)

Any help will be appreciated
Thanks in advance

Define CORS values in the site config file

2 Likes

In site_config.json, you can set allow_cors to required value so that cross-site HTTP requests are allowed. You can read more about it here.

https://frappeframework.com/docs/user/en/basics/site_config#optional-settings-2

1 Like

Well i just forgot to mention that but it is defined already in site_config:
“allow_cors”: “*”
and it is working for the built-in api end points

It is set already
“allow_cors”: “*”
but only working with the built-in api endpoints not my custom endpoints !!!

Well for some odd reason it just worked after resetting random configurations.

What do you mean by random configurations

Re-configuring nginx: bench setup nginx
Letsencrypt certificate renewal
I don’t know what worked exactly