1

I am working with python pyramid and basically I have defined a route like this:

config.add_route('metaschemaxml', '/metaschema/{id}/xml')

The view mapping metashemaxml is like this:

@view_config(route_name='metaschemaxml', renderer='string')
def metaxml_view(request):
  schema = request.matchdict['id']
  urlparams = request.query_string
  urlparams.strip()
  required = 0
  for x in urlparams.split(','):
      if "required=1" in x:
          required = 1
  rxml = '<?xml version="1.0" encoding="utf-8"?><eroot></eroot>'

  try:
      tags = DBSession.query(mtemplatexelem_model).filter(mtemplatexelem_model.template_id == int(schema)).order_by(mtemplatexelem_model.xelem_id).all()
      rxml = getXMLFromQuery(tags, required)
  except DBAPIError:
      return Response("Error in DB", content_type='text/plain', status_int=500)

  return Response(rxml, content_type='text/xml', charset='utf8')

All works well if for example I call:

http://localhost:6543/metaschema/1/xml

But if I do the same request by Ajax I get:

XMLHttpRequest cannot load http://172.26.16.28:6543/metaschema/1/xml. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

What do I need to do to allow the ajax request in pyramid?

Thanks, Carlos

QLands
  • 2,424
  • 5
  • 30
  • 50

2 Answers2

3

The localhost and 172.26.16.28 are different domains, and the majority of browsers do not allow cross domain AJAX requests. More on that subject and possible solutions you can find here: Origin is not allowed by Access-Control-Allow-Origin.

Community
  • 1
  • 1
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
1

Thanks Alexander for the explanation.

To solve this in pyramid I changed this in my code

resp = Response(rxml, content_type='text/xml', charset='utf8')
resp.headerlist.append(('Access-Control-Allow-Origin', '*')) #Add the access control
return resp
QLands
  • 2,424
  • 5
  • 30
  • 50