首页 > 解决方案 > 从 AJAX 向 Flask 发出 POST 请求时出现 500 响应

问题描述

下面是我的functions.js文件

var count = 1;

// get a tweet from backend
function display_tweet(){

    var req = {
        "index": count
    }
    .ajax({
    type: "POST",
    url: "get_tweet",
    data: '{"id":"' + count+'}',
    dataType: "json",
    success: function(data) {
        alert(data.d);
    },
    error: function(data){
        alert("fail");
    }
});
}

下面是我的 python app.py 文件

@app.route('/get_tweet',methods = ['POST'])
def get_tweet():


    #user_agent_received = request.get_json()
    #print("request is ",request.form)
    print("request ",request.get_json())

    global data_dict
    print(data_dict)
    input_map = dict(request.form.to_dict())
    print(input_map)
    index=input_map['index']
    index=int(str(index))
    final_dic={}
    final_dic["text"]=data_dict[index]["tweet"]["full_text"]
    return json.dumps(final_dic)

下面是我的 index.html 文件中的存根

<div class="tweet">
            <div class="data">
                <button onclick="display_tweet()">display a new tweet</button>
                <div id="fulltextt"></div>
            </div>

我不断在后端收到一个带有 500 个内部错误的空 json,看起来像这样。请有人帮忙

{}
127.0.0.1 - - [04/Feb/2020 23:02:07] "POST /get_tweet HTTP/1.1" 500 -

标签: python-3.xajaxpostflaskhttp-status-code-500

解决方案


推荐阅读