将Ajax从Java脚本发送到flask

时间:2018-11-28 23:23:41

标签: python json ajax python-3.x flask

我有一个很大的表格数据形式,我已经用Javascript将其转换为JSON对象,但是无论何时我将其发送给python,无论我做什么,它总是作为大字符串对象导入

jS数据:

在页面顶部

const ajaxRequest = new XMLHttpRequest();
ajaxRequest.open('post' , "/addToTable");
ajaxRequest.onload = ()=>{
var parseRespons = JSON.parse(ajaxRequest.responseText);

在底部:

 const newJson = JSON.parse(jsonData);
 console.log(newJson[0]); // return a json object with the right formate
 const selectedTypeData = document.querySelector("#upType").value;

const fd = new FormData();

fd.append('selectedTypeData',selectedTypeData);
fd.append('newJson',newJson);


ajaxRequest.send(fd);
return false;

在python上:

这是我尝试过的方法

@app.route('/addToTable',methods=['POST'])
def addToTable():
print("active")
jD = requests.json("newJson") # print(jD[0]) ---> [object Object],[object Object],[object Object]

data = request.get_json() # print(jD) ---> None 
#jDD = request.form.get("jsonData") # print(jD[0]) ---> [
#jD_ = jsonify(jD)  # print(jD) ---> <Response 3219 bytes [200 OK]>
#jD = json.loads(response.text) #print(jD) --->  None

我不知道哪种方法将其作为JSON对象导入python。

1 个答案:

答案 0 :(得分:0)

import json

jS = request.form.get("jsonData")
jD = json.loads(jS)

为我解决了这个问题..在我一生的24小时内付出了努力,以找出答案