0

I'm having some troubles and I hope you can help me. I've configured the RPi with the Cam Web Interface, who listens to port 80 in the apache server.

I also have to use python to get some information from the RPi to the web interface (using Flask) but it cannot use the port 80, so it's using the port 5000. Everything working fine.

I had done a simple scrip with ajax to request data in json to the python module.

But I'm receiving the following error:

169.254.203.139 - - [07/Jul/2015 01:16:31] "GET /flaskr HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/flaskr/static/templates/hello-flask.py", line 7, in hello
    return jsonify(myList)
  File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 237, in jsonify
    return current_app.response_class(dumps(dict(*args, **kwargs),
TypeError: cannot convert dictionary update sequence element #0 to a sequence 

Here is my code:

html:

<head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min$
</head>

<button onclick="test()">
        Test
</button>
<script>
function test(){
   $.getJSON('http://raspberrypi.local:5000/flaskr', {
}, function(data){
alert(data);
alert('JSON posted: ' + JSON.stringify(data));
     // Handles the callback when the data returns
});
}
</script>
<div id="test">
</div>

error from the console:

XMLHttpRequest cannot load http://raspberrypi.local:5000/flaskr. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://raspberrypi.local' is therefore not allowed access. The response had HTTP status code 500.

python code:

from flask import Flask, render_template, request, jsonify
app = Flask(__name__)

@app.route("/flaskr")
def hello():
        myList=[1,2,3,4,5,6]
        return jsonify(myList)

if __name__=="__main__":
        app.run(host='raspberrypi.local', debug=True)

1 Answers1

0

Take a look at this link, it explains how Access-Control-Allow-Origin works.

You might be hosting it on one url and directing ajax to another, and browsers have a same origin policy preventing this from working.

Community
  • 1
  • 1
Hrvoje
  • 362
  • 2
  • 6