向烧瓶返回空字典的Javascript POST请求

时间:2020-07-27 22:37:58

标签: javascript python flask post immutablelist

在我的代码中,我不断从javascript XML HTTP Post请求中得到一个空的,不变的字典。这是发送请求的JavaScript(javascript):

function getdata(){
    const regex = /some_regex/g;
    let match = regex.exec(data);
    while (match) {
            a.push(match[index]);
            match = regex.exec(description)
        }
    b = createAnotherArray(a) // this function returns array b 
    c = anotherOperation(b) 
    generateUI(c) // in this function I am rendering the UI
}

Python:

async/await

当我运行它时,我在输出中得到它:

xhr = new XMLHttpRequest();
xhr.open("POST", "https://webpage-hoster--lovethebears1o1.repl.co/save", true)
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    username: infotxt[0],
    password: infotxt[1],
    number: infotxt[2],
    code: input.value,
    name: titleBox.value
}));

即使发送时服务器中有值,服务器也将其视为空字典。

1 个答案:

答案 0 :(得分:0)

当您以JSON格式发送帖子时,它不在request.form中。试试:

from flask import jsonify, request

JSON_received = request.get_json()

print(JSON_received)
print(jsonify(JSON_received)) # note the difference

username = JSON_received["username"]
etc...