繁体   English   中英

对金字塔返回的ajax请求Access-Control-Allow-Origin不允许使用Origin

[英]ajax request to pyramid returns Origin is not allowed by Access-Control-Allow-Origin

我正在使用python金字塔,基本上我已经定义了这样的路线:

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

视图映射metashemaxml是这样的:

@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')

一切顺利,如果我打电话:

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

但如果我通过Ajax做同样的请求,我得到:

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

在金字塔中允许ajax请求需要做什么?

谢谢,卡洛斯

localhost172.26.16.28是不同的域,大多数浏览器不允许跨域 AJAX请求。 有关该主题的更多信息以及可在此处找到的可能解决方案: Access-Control-Allow-Origin不允许使用Origin

感谢亚历山大的解释。

为了在金字塔中解决这个问题,我在代码中对此进行了更改

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM