前端:html5(vs code环境下编写 没用框架)
后端:python flak框架写的接口
数据库:SQL server
问题:简单的登录页面,输入正确的用户名和密码之后,点击登录报错:Failed to load resource: the server responded with a status of 405 ()
后端接口提示:
"OPTIONS /api/login?user=admin&passwd=LCQHWaOtpk8%3D HTTP/1.1" 200 -
"GET /api/login?user=admin&passwd=LCQHWaOtpk8%3D HTTP/1.1" 200 -
搜了百度,405应该是跨域问题,用了一位网友的方法,但是没有解决,还是405
from flask_cors import *
app = Flask(__name__)
CORS(app, supports_credentials=True, resource=r'/*')
前端ajax代码:
$.ajax({
type:"GET",
url:"http://192.168.8.241:8888/api/login",
data:{"user":$('#username').val(),"passwd":$('#password').val()},
dataType:'json',
success:function(resu){ //服务器(接口)返回来的数据
if(typeof(resu.msg) =="success"){
window.location.href="index.html"
window.event.returnValue=false;
}
if(typeof(resu.msg)=="fail"){
alert("登录失败!");
}
},
error:function(XMLHttpRequest,textStatus,thrownError){
}
});
后端 login.py代码
@app.route("/api/login",methods=["get"])
def login():
"""登录页:用户名密码"""
username = str(request.values.get("user"))
password = str(request.values.get("passwd"))
sql = "SELECT * FROM fp.dbo.users WHERE userm='"+username+"'and userpwd='"+password+"'"
data = ssql_operate.db.select_db(sql)
if data:
resu = {'code': 200, 'msg': 'success','token':'135456784'}
return json.dumps(resu, ensure_ascii=False)
else:
resu ={'code': -1, 'msg': 'fail'}
return json.dumps(resu, ensure_ascii=False)