我正在尝试在flask服务器和html页面之间开始一些通信。我在此处http://flask.pocoo.org/snippets/56/中提供了跨域代码,但仍然无效。这是我的python代码:
from flask import *
from crossdomain import *
app = Flask(__name__)
@app.route('/')
@crossdomain(origin='*')
def pocetna():
return '1'
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8081,debug=True)
这是我的javascript:
function prebaci(){
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText==1) document.getElementById("kuca").innerHTML="RADI";
else document.getElementById("kuca").innerHTML="NE RADI";
}
}
xmlhttp.open("GET","127.0.0.1:8081",true);
xmlhttp.send();
}
谷歌浏览器错误是:
XMLHttpRequest cannot load %3127.0.0.1:8081. Cross origin requests are only supported for HTTP.
在Mozzila Firefox中:
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
答案 0 :(得分:1)
请注意特定的错误消息,它告诉您没有连接到HTTP服务器;至少Chrome不这么认为。
使用:
xmlhttp.open("GET","http://127.0.0.1:8081/",true);
e.g。使用合适的URL。