0

I have a problem with my CherryPy install on my raspberry.

When I request an function of CherryPy with ajax I have this message :

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.

My python code :

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())

How change header response with CherryPy ?

Thanks by advance =)

1 Answers1

4

Try changing your startup to:

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)
ccarton
  • 3,556
  • 16
  • 17