Ajax不向烧瓶发送参数

我正在构建一个Flask应用程序,并且正在使用jquery发送ajax请求。我在同一html页面上为两个不同的按钮创建了两个类似的ajax请求。我已经为两个ajax请求完成了相同的编码,但是一个正在工作,而另一个则没有。从我的感觉,不工作的ajax请求没有在后请求中发送参数。 这是烧瓶代码

@app.route('/createOrder',methods=['POST','GET'])
def createOrder():
    username = chatusersession[session.get("uname")][1]
    funct = request.form.get('funct')
    if funct == "Create Order":
        if username in ordersession:
            del ordersession[username]
        return render_template('createOrder.html',orderfor=username)
    if funct == "getPaperCode":
        paper_id = request.form.get("papercode")
        crscode = db1.execute("Select course_code from student_master where rollno= :username ",{"username":username}).fetchone()
        try:
            query = db1.execute("Select pap_code,pap_name from subject_master where pap_code= :paper_id and course_code= :crscode",{"paper_id":paper_id,"crscode":crscode[0]}).fetchone()
            if query is None:
                return jsonify({"succes":False,"msg":"No Paper Code found!"})
            else:
                if username not in ordersession:
                    ordersession[username] = {"papers":[query[0],],"discount":None}
                    return jsonify({"succes":True,"papercode":query[0],"papername":query[1]})

                else:
                    if query[0] in ordersession[username]["papers"]:
                        return jsonify({"succes":False,"msg":"You have already added this paper"})
                    else:
                        ordersession[username]["papers"].append[query[0]]
                        return jsonify({"succes":True,"papercode":query[0],"papername":query[1]})
        except:
            return jsonify({"succes":False,"msg":"Can not lookup! Try again Later."})
    if funct == "deletepaper":
        paper_id = request.form.get("papercode")
        if paper_id in ordersession[username]["papers"]:
            ordersession[username]["papers"].remove(paper_id)
            return jsonify({"succes":True,"msg":"Deleted Successfuly"})
        else:
            return jsonify({"succes":False,"msg":"Can not delete , Some error"})

这是javascript,顶部定义的ajax函数无法正常工作,而第二个ajax函数可以正常工作..发送请求的代码相同

$(document).ready(function(){
    $('#deletepaper').on('submit',function(event) {

        $.ajax ({
            data: {
                papercode : $('#papercodeinput').val() ,
                funct : $('#functid').val()
            },
            method : 'POST',
            url : '/createOrder'
        })
        .done(function(data){
            if(data.succes) {
                console.log("Hello success");
                console.log(data.msg);
                $('.table tbody').on('click','.btn',function(){
                    $(this).closet('tr').rmeove();
                });
            }
            else {
                console.log("Hello not success");

                document.getElementById("alert").innerHTML = data.msg;
            }

        });
    });
    });
$(document).ready(function(){
    $('#getpaper').on('submit',function(event) {

        $.ajax ({
            data: {
                papercode : $('.papercodeinput').val() ,
                funct : $('#functid').val()
            },
            type : 'POST',
            url : '/createOrder'
        })
        .done(function(data){
            if(data.succes) {
                console.log("Hello success");
                console.log(data);
                var table = document.getElementById("paperlist");
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                cell1.innerHTML = `<form id="deletepaper"><input type="hidden" id="papercodeinput" value=${data.papercode}><input type="hidden" id="functid" value='deletepaper'><button type="submit" class="btn">X</button></form>`;
                cell2.innerHTML = data.papercode;
                cell3.innerHTML = data.papername;
                cell4.innerHTML = "<input>"
            }
            else {
                console.log(data.msg);
            }

        });
        document.querySelector('.papercodeinput').value = "";
        event.preventDefault();
    });

});

html代码是

<div class='container'>
            <div class="order">
                <p><h3>Order for : {{ orderfor }}</h3></p>
                <form id="getpaper">
                <label>Paper Code</label>
                <input type="text" class='papercodeinput'>
                <p id="alert"></p>

                <input type='hidden' id="functid" name='funct' value='getPaperCode'>
                <button type="submit" id="addpaper" name='addpaper'>Add Paper</button>
                </form>
                <table id="paperlist">
                    <thead>
                    <tr>
                        <th>F</th>
                        <th>PaperCode</th>
                        <th>Paper Name</th>
                        <th>Expected Marks/Grade</th>

                    </tr>
                </thead>
                <tbody>

                </tbody>
                </table> 

处理第一个Ajax请求时,flask抛出错误

“视图函数未返回有效响应。 TypeError:视图函数未返回有效响应。该函数返回None或在没有return语句的情况下结束。127.0.0.1“

Which I understand is because when ajax reaches server there is no return request received. And on server side "if funct == "deletepaper":" this code receives this ajax request . I think the parameter is not being passed to server that is why non of the if query works and nothing is returned .. Please help .. Thanks in advance :)

评论