gpt4 book ai didi

javascript - 错误 405 : Method Not allowed flask, ajax

转载 作者:行者123 更新时间:2023-12-03 05:42:46 25 4
gpt4 key购买 nike

只是尝试使用 Ajax 和 Python-Flask 将表单数据提交到 MySQL DB,但同样的错误“方法不允许”一次又一次出现..请查看代码并帮助我......

<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title" Align="center">Create Account</h4>
</div>
<form role="form" method="POST">
<div class="modal-body">
<p id="msg"></p>
<input class="form-control" name="email" type="text" Placeholder="Email"></input><br />
<input class="form-control" name="firstname" type="text" Placeholder="First Name"></input><br />
<input class="form-control" name="lastname" type="text" Placeholder="Last Name"></input><br />
<input class="form-control" name="password" type="password" Placeholder="Password"></input><br />
<input class="form-control" type="password" Placeholder="Confirm Password"></input>
</div>
<div class="modal-footer">
<button id="btncheck" class="btn btn-danger" type="submit" >Signup</button>
</div>
</form>
</div>

</div>
</div>
</div>

<script>

$(function(){

$("#btncheck").submit(function(){
$.ajax({
url: '/signup',
type: 'POST'
success: function(res){
console.log(res);

}

});
});

Python 代码

@app.route("/signup", methods=['POST','GET'])
def signup():
try:
if request.method == 'POST':
_email =request.form["email"]
_firstname = request.form["firstname"]
_lastname = request.form["lastname"]
_password = request.form["password"]
con = mysql.connect()
cursor = con.cursor()
if _email and _firstname and _password:
cursor.execute("""INSERT INTO signpp (
user_email,
user_fname,
user_lname,
user_password)
VALUES (%s,%s,%s,%s)""",(_email,_firstname,_lastname,_password))

data = cursor.fetchall()
if len(data) is 0:
con.commit()
return jsonify(data = "User created successfully !")
else:
return jsonify(str(data[0]))


except Exception as e:
return render_template('error.html', error = str(e))

cursor.close()
con.close()

错误:方法不允许

所请求的 URL 不允许使用该方法。

最佳答案

您的 Web 服务响应您的 ajax 调用是否需要任何数据,我认为自从您执行 POST 以来就是如此?如果是这样,我看不到您将其传递到哪里,并且可能会抛出此错误(不匹配签名)。数据:yourDataHere

举个例子:

var params = { StateId: stateid };

$.ajax({
dataType: 'json',
type: 'post',
url: 'YOURURLHERE',
data: params,
error: function (request, status, error) {
alert('Error: ' + error);
},
success: function (result) {
$('#body').html(result);

}
});

关于javascript - 错误 405 : Method Not allowed flask, ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com