如何修复Flask Python中的“未找到此页面”?

时间:2019-06-20 19:15:22

标签: python jquery ajax flask werkzeug

我正在Flask中为API设置新服务器。还有一台在Flask中渲染前端的服务器。好的,所以当我请求确定API路由时,会得到这个奇怪的响应“找不到此页面”。我真的似乎一切正常,我该如何调试该错误?另一个奇怪的事情是,它始终使我的状态为200 OK。错误出在路由中: @ app.route('/ v1.0 / aluno / update /',methods = ['POST'])

API侧


@app.route('/v1.0/aluno/<int:aluno_id>', methods=['GET'])
def aluno(aluno_id):
    if request.method == 'GET':
        cur = mysql.connection.cursor()
        query = "SELECT NOME, NUMERO, PASSWORD FROM aluno WHERE NUMERO=%s"
        cur.execute(query, (aluno_id,))
        data = cur.fetchall()
        if len(data) <= 0:
            return Response(status=404)
        else:
            aluno = {
                'nome': data[0][0],
                'numero': data[0][1],
                'password': data[0][2]
            }
            js = json.dumps(aluno)
            resp = Response(js, status=200, mimetype='application/json')
            resp.headers['Links'] = 'http://127.0.0.1/aluno'
            return resp

@app.route('/v1.0/aluno/delete/<int:aluno_id>', methods=['POST'])
def aluno_delete(aluno_id):
    if request.method == 'POST' and request.form['_method'] == 'delete':
        query = "DELETE FROM aluno WHERE NUMERO = %s"
        cur = mysql.connection.cursor()
        cur.execute(query, (aluno_id,))
        mysql.connection.commit()
        cur.fetchall()
        cur.close()
        return Response(status=200)

@app.route('/v1.0/aluno/update/<int:aluno_id>', methods=['POST'])
def aluno_update(aluno_id):
    form = AlunoForm(request.form)
    if request.method == 'POST' and form.validate():
        nome = request.form["nome"]
        numero = request.form["numero"]
        password = request.form["password"]
        cur = mysql.connection.cursor()
        query = "UPDATE aluno SET NOME=%s, NUMERO=%s, PASSWORD=%s WHERE NUMERO = %s"
        cur.execute(query, (nome, numero, password, aluno_id))
        mysql.connection.commit()
        cur.execute(
            "SELECT NOME, NUMERO FROM aluno WHERE NUMERO = %s", (aluno_id,))
        data = cur.fetchall()
        cur.close()

        print(" * DATA ")
        print(data)

        aluno = {
            'nome': data[0][0],
            'numero': data[0][1]
        }

        js = json.dumps(aluno)
        resp = Response(js, status=200, mimetype='application/json')
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp
    elif request.method == 'POST' and not form.validate():
        resp = Response(status=400)
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp

首尾端


{% endblock %}


<script type="text/javascript" src="{{url_for('static', filename='js/jquery-3.2.1.min.js') }}"></script>

<script type="text/javascript" src="{{url_for('static', filename = 'js/bootstrap.min.js')}}"></script>

<script>
    function aluno_update(){
        try{
            let formElement = document.getElementById("aluno_update")
            //let formData = formElement.
            //console.log(formData)
            $.ajax({
                type: "POST",
                url: "http://127.0.0.1:80/v1.0/aluno/update/{{aluno['numero']}}",
                data: {'nome': 'João Luis','numero':'16172','password':'Password'},
                //dataType: 'json',
                success: function(data){
                    //location.href = "http://127.0.0.1:3000/v1.0/alunos/"
                    alert(data)
                    console.log(data)
                },
                error(jqXHR,JQueryXHR,errorThrown){
                    //console.log(formData)
                    alert(jqXHR)
                    alert(JQueryXHR)
                    alert(errorThrown)
                    console.log(jqXHR)
                    console.log(JQueryXHR)
                    console.log(errorThrown)
                }
            })
        }catch(err){
            alert(err)
        }
    }
</script>

我尝试使用POSTMAN而不是常规的Web浏览器。向API发出请求。但是我得到了相同的答复:“未找到此页面”

至少我期望有400错误请求之类的东西。

https://github.com/joaogracio/SqlParser

1 个答案:

答案 0 :(得分:1)

表单验证失败,根据下面的代码您将获得400

   elif request.method == 'POST' and not form.validate():
        resp = Response(status=400)
        resp.headers['Links'] = 'http://127.0.0.1/aluno'
        return resp