WebDev CORS error in V8.1.5

I tried to develop a webdev api. And When I create a post method, when I request from client, I entered a cors error. Then I tried to add a cors header like this:

servletResp = request[‘servletResponse’]
servletResp.addHeader(‘Access-Control-Allow-Origin’, ‘*’ )
servletResp.setHeader(“Access-Control-Allow-Methods”, “POST, GET, PUT, OPTIONS, DELETE”)

It works in v7.9 and v8.0, but now I use v8.1.5 and it seems dosen’t work.
Need help!

Are you only implementing doPost? To properly support CORS, you should be implementing doOptions to return your rewritten CORS headers.

1 Like

Can you share an example for this? I cannot find any examples.

The original post has a script that looks like it should work, as long as it's implemented in the right HTTP method. What header values you want to return are up to you.

I created a python resource on Web Dev Module and implemented the doGet method as the following. As you can see it returns a json list object with the list of tags that has history.

I'm trying to develop a html/js/jquery app on my local dev machine (mind that dev machine is different than the server). The JS Fetch API is throwing the CORS error.

How can I overcome this to allow CORS error to allow any origin temporarily (until I finish development)?


def doGet(request, session):
	tags = list(parse_tags())
	return { "json": tags }
	
def parse_tags():
	tags = system.tag.browseTags(parentPath="", recursive=True)
	
	for tag in tags:
		path = str(tag.fullPath)
		
		has_history = parse_has_history(path)
		
		if not has_history:
			continue
		
		name = str(tag.name)
		type = str(tag.tagType)
		data_type = str(tag.dataType)
		
		yield { 
			"path": path, 
			"name": name, 
			"type": type, 
			"data_type": data_type, 
			"has_history": str(has_history),
			"check": False,
			}

def parse_has_history(path):
	try:
		return len([x for x in system.tag.getConfiguration(path) if x["historyEnabled"] == True]) > 0
	except:
		return False

Here is my code doGet method below. I'm trying to return list of interest tags as json list.

In another environment I'm trying to develop html/jquery app that utilizes this function, but JS Fetch API is getting CORS error.

How can I overcome this? Any help would be much appreciated.

def doGet(request, session):
	tags = list(parse_tags())
	return { "json": tags }
	
def parse_tags():
	tags = system.tag.browseTags(parentPath="", recursive=True)
	
	for tag in tags:
		path = str(tag.fullPath)
		
		has_history = parse_has_history(path)
		
		if not has_history:
			continue
		
		name = str(tag.name)
		type = str(tag.tagType)
		data_type = str(tag.dataType)
		
		yield { 
			"path": path, 
			"name": name, 
			"type": type, 
			"data_type": data_type, 
			"has_history": str(has_history),
			"check": False,
			}

def parse_has_history(path):
	try:
		return len([x for x in system.tag.getConfiguration(path) if x["historyEnabled"] == True]) > 0
	except:
		return False

You will have to do research on simple vs preflighted requests. You might have to implement the doOptions header, depending on the exact nature of the request you're trying to serve with Webdev.

First, have you use Postman? for testing your request.

I have the similar issue.

Postman works fine but React.js not good, having the CORS issue.