Enable CORS for WebDev

I have a Web app that i am trying to make ajax calls to an ignition WebDev. These call are failing on the options call. These are in another domain, so need to enable the option replay and enable CORS to allow * orgins.

On Linux Ignition 8.0.15. How do i enable CORS allow * orgins, the other CORS related headers for all webdev call made?

I tried editing the web.xml file, but Ignition failed to start after copy paste of xml from another post. That post was related to an older version of Ignition. So guessing is not longer valid method.

There’s no blanket way to do it, but you can implement a doOptions header that returns the values you need. I’d recommend doing it in a project script, so you can just copy the call to that function into each Python handler you have.

do you have any example?

Your actual response headers will change based on the input request and what you want to allow it to do.
The basic structure should be something like this:

from javax.servlet.http import HttpServletResponse

servletResponse = request["servletResponse"]
servletResponse.addHeader(someCorsHeaders, someCorsValue)
# other headers based on incoming request
servletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT)

See Cross-Origin Resource Sharing (CORS) - HTTP | MDN for reference on what to send in the response.

1 Like

Hi Paul,

Thank you for your guide.
I manage to enable CORS in my WebDev doGet() with the below codes.

Hope this will help others 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.
1 Like