错误405:方法不允许使用flask,ajax

时间:2016-11-07 12:41:10

标签: javascript python ajax python-2.7 flask

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

<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()

错误:方法不允许

请求的网址不允许使用该方法。

2 个答案:

答案 0 :(得分:1)

你的web服务响应你的ajax调用是否需要任何数据,我假设你做了一个POST?如果是这样,我没有看到你传入的位置,并且可能抛出此错误(不匹配的签名)。 data: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);

                        }
});

答案 1 :(得分:0)

我确定是否是拼写错误,但你在type: 'POST'之后忘了逗号。

在旁注中,您将以明文形式发送敏感数据,例如用户密码。我建议先加密它。最常见的技术之一是bcrypt