CherryPy访问控制允许来源

Nic*_*ens 0 python ajax cherrypy

我在树莓派上安装CherryPy时遇到问题。

当我请求带有ajax的CherryPy函数时,出现以下消息:

XMLHttpRequest cannot load http://my_ip:8888/takePicture. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my_ip' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

我的python代码:

import time
import os
import cherrypy

class Camera(object):
    @cherrypy.expose
    def takePicture(self):
        os.system('fswebcam -r 1280x720 -S 3 --info Salon --jpeg 100 --save images/history/%H%M%S.jpg')
        os.system('fswebcam -r 1280x720 -S 3 --info Salon --jpeg 100 --save images/last.jpg')

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_host': 'my_ip'})
    cherrypy.config.update({'server.socket_port': 8888})
    cherrypy.quickstart(Camera())
Run Code Online (Sandbox Code Playgroud)

如何使用CherryPy更改标头响应?

预先感谢=)

cca*_*ton 5

尝试将您的启动更改为:

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type', 'image/jpeg'), ('Access-Control-Allow-Origin', 'http://my_ip')],
            'server.socket_host': 'my_ip',
            'server.socket_port': 8888
        }
    }
    cherrypy.quickstart(Camera(), '/', conf)
Run Code Online (Sandbox Code Playgroud)