How to enable the CORS on the web dev module

Hi,
I am trying to enable the CORS on the web dev module, but I am not able to get it working. I have set up the CORS headers in the doOptions method, but I am still encountering the same CORS issue on the frontend. Additionally, I copied the web dev endpoint example from the Github, but I am facing the same issue.
The code I used in the doOptions method is as follows:

def doOptions(request, session):
 	servletResp = request['servletResponse']
	servletResp.setHeader("Access-Control-Allow-Origin", "http://localhost:3000")
	servletResp.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE")
	servletResp.setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization, X-Requested-With")
	return {'json': {}}

If you're wanting to use the servletResponse, you'll need to

return None

as your last line. (If you do want to return some json, you'll want to add that to the servletResponse body, instead of returning a dictionary.)

I'm not sure if that's your only issue, but I hope it points you in the right direction!

1 Like

Hi,

I managed to enable CORS in my WebDev doGet() with the below codes.

Hope this will help you too.

#modify response headers
servletResponse = request["servletResponse"]
servletResponse.addHeader("Access-Control-Allow-Origin", "*") #allow API calls from other hosts in addition to that of the gateway

tag = system.tag.readBlocking(['[default]zTest/int2'])[0].value
json = {'tag': tag}
return {'json': json}

#This response can be retrieved with fetch() in JavaScript.