我遇到了一些麻烦,希望你能帮助我。我已经使用Cam Web Interface配置了RPi,后者在apache服务器中侦听端口80。
我还必须使用python从RPi获取一些信息到Web界面(使用Flask),但它不能使用端口80,所以它使用端口5000.一切正常。
我用ajax做了一个简单的脚本,以便在json中向python模块请求数据。
但我收到以下错误:
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
这是我的代码:
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>
来自控制台的错误:
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代码:
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)
答案 0 :(得分:0)
查看this link,它解释了Access-Control-Allow-Origin的工作原理。
您可能将其托管在一个网址上并将ajax指向另一个网址,并且浏览器具有相同的原始策略,阻止其工作。