我需要通过XavascriptHttpRequest从javascript向python服务器发送数据.因为我使用的是localhost,所以我需要使用CORS.我正在使用Flask框架及其模块flask_cors.作为javascript我有这个:
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", "http://localhost:5000/signin", true);
var params = "email=" + email + "&password=" + password;
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
Run Code Online (Sandbox Code Playgroud)
和python代码:
@app.route('/signin', methods=['POST'])
@cross_origin()
def sign_in():
email = cgi.escape(request.values["email"])
password = cgi.escape(request.values["password"])
Run Code Online (Sandbox Code Playgroud)
但当我执行它时,我收到此消息:
XMLHttpRequest无法加载localhost:5000/signin.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.
我该如何解决?我知道我需要使用一些"Access-Control-Allow-Origin"标头,但我不知道如何在此代码中实现它.顺便说一句,我需要使用纯JavaScript.