I'm getting this error on chrome Access to XMLHttpRequest at 'http://localhost:8069/api/project.budget' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
@http.route(
'/api/<string:model>',
type='http', auth='user', methods=['OPTIONS', 'GET'], csrf=False, cors='*')
def get_model_data(self, model, **params):
try:
records = request.env[model].search([])
except KeyError as e:
msg = "The model `%s` does not exist." % model
res = error_response(e, msg)
return http.Response(
json.dumps(res),
status=200,
mimetype='application/json'
)
if "query" in params:
query = params["query"]
else:
query = "{*}"
if "filter" in params:
filters = json.loads(params["filter"])
records = request.env[model].search(filters)
prev_page = None
next_page = None
total_page_number = 1
current_page = 1
if "page_size" in params:
page_size = int(params["page_size"])
count = len(records)
total_page_number = math.ceil(count/page_size)
if "page" in params:
current_page = int(params["page"])
else:
current_page = 1 # Default page Number
start = page_size*(current_page-1)
stop = current_page*page_size
records = records[start:stop]
next_page = current_page+1 \
if 0 < current_page + 1 <= total_page_number \
else None
prev_page = current_page-1 \
if 0 < current_page - 1 <= total_page_number \
else None
if "limit" in params:
limit = int(params["limit"])
records = records[0:limit]
try:
serializer = Serializer(records, query ,many=True)
data = serializer.data
except (SyntaxError, QueryFormatError) as e:
res = error_response(e, e.msg)
return http.Response(
json.dumps(res),
status=200,
mimetype='application/json'
)
res = {
"count": len(records),
"prev": prev_page,
"current": current_page,
"next": next_page,
"total_pages": total_page_number,
"result": data
}
return http.Response(
json.dumps(res),
status=200,
mimetype='application/json'
)