关于python的bottle框架跨域请求报错问题的处理

  在用python的bottle框架开发时,前端使用ajax跨域访问时,js代码老是进入不了success,而是进入了error,而返回的状态却是200。url直接在浏览器访问也是正常的,浏览器按F12后会发现下面这个错误提示

XMLHttpRequest cannot load http://192.168.0.118:8081/get_mobile_number/?id=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

  通过搜索引擎查询错误,会发现几乎查找出来的答案都说是跨域问题,只需要在主文件的代码中添加下面就可以了,国外的网站好多解决方案都是这样说明

1
2
3
@hook ( 'after_request' )
def  enable_cors():
     response.headers[ 'Access-Control-Allow-Origin' =  '*'

  而事实上是按找出来的解决方法添加后还是出现错误,查看浏览器输出的http头并没有看到我们刚刚增加的Access-Control-Allow-Origin:*,如下图:

 

  通过DEBUG,进入bottle的源码中查看

 

  这个问题我测试过在python2与python3对应的bottle框架中都存在这种问题,我们将它改为:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  HTTPResponse(Response, BottleException):
     def  __init__( self , body = '', status = None , headers = None * * more_headers):
         super (HTTPResponse,  self ).__init__(body, status, headers,  * * more_headers)
 
     def  apply ( self , response):
         response._status_code  =  self ._status_code
         response._status_line  =  self ._status_line
         if  self ._headers:
             if  response._headers:
                 response._headers.update( self ._headers)
             else :
                 response._headers  =  self ._headers
 
         response._cookies  =  self ._cookies
         response.body  =  self .body

  

  

  再运行代码就可以看见ajax代码正常了

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值