Back

CORS issue with new functions custom domain

  • 0
  • Functions
Drake
9 Sep, 2023, 20:18

So that's in your function code?

TL;DR
User is experiencing a CORS issue with a new custom domain for their functions. They tried implementing the options method but encountered a problem when the function times out or an unexpected error occurs. They were able to solve the issue by setting the Access-Control-Allow-Origin and Access-Control-Allow-Headers headers. There is a discussion about the runtime being used and how to set headers when using res.json in Appwrite functions. The user tests the function code and confirms that the Allow-Origin header works. They also mention that there is no response, only CORS errors, and suspect that the browser may be hiding the request. There is a suggestion to check if the URL
SenZi
9 Sep, 2023, 20:18

yes

SenZi
9 Sep, 2023, 20:19

there is no response, just cors errors

Drake
9 Sep, 2023, 20:20

Is that first one an option request?

SenZi
9 Sep, 2023, 20:21

i think its just react calling the post request two times

Drake
9 Sep, 2023, 20:22

Is there an options request further back?

SenZi
9 Sep, 2023, 20:23

well right before, i am getting the user's jwt token to pass into the function, that is an option request

Drake
9 Sep, 2023, 20:25

Ugh I wonder if the request is hidden by the browser...

Does the url work if you just open it in the browser url?

SenZi
9 Sep, 2023, 20:28

yes it works if i just open it or in postman

SenZi
9 Sep, 2023, 20:31

oh there is an options request right after it

Drake
9 Sep, 2023, 20:32

Did it fail? What are the details for it?

SenZi
9 Sep, 2023, 20:33

nope it doesnt seem to fail

SenZi
9 Sep, 2023, 20:33

what details? there is no response or anything so

Drake
9 Sep, 2023, 20:34

Maybe try again? Sometimes the browser clears out the response

SenZi
9 Sep, 2023, 20:35

there is no response

Drake
9 Sep, 2023, 21:08

So I'm actually starting to think we don't automatically send back that allow origin header for these function routes. Can you try updating your function code to set the allow origin header to * for testing?

SenZi
9 Sep, 2023, 21:23

in appwrite functions how do you set headers when using res.json

Drake
9 Sep, 2023, 22:25

What's your runtime?

SenZi
9 Sep, 2023, 22:31

node js 18

SenZi
9 Sep, 2023, 22:39

ohk got it

SenZi
9 Sep, 2023, 22:53

Yep it works now. setting {"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Content-Type"} works.

SenZi
10 Sep, 2023, 01:12

@Steven but there is an issue, if the function times out or if some unexpected error happens the cors issue would happen since the header is not set in that case

Drake
10 Sep, 2023, 01:46

Can you try to implement the options method to only return the allow headers?

SenZi
10 Sep, 2023, 02:22

Will give it a try

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more