亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript POST 請求 Flask 返回空字典

Javascript POST 請求 Flask 返回空字典

婷婷同學_ 2022-12-09 15:17:39
在我的代碼中,我一直從我的 javascript XML HTTP Post 請求中獲取一個空的不可變字典。這是發送請求的 javascript (javascript):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}));蟒蛇:@app.route("/save", methods=["POST"])def save():  try:    print(str(request.form))    username = str(request.form["username"])    pword = str(request.form["password"])    number = int(request.form["number"])    code = str(request.form["code"])    name = str(request.form["name"])  except Exception as e:    print("error-1-" + str(e))    return "error"return "success"當我運行它時,我在輸出中得到了這個:ImmutableMultiDict([])error-1-400 Bad Request: The browser (or proxy) sent a request that this server could not understand.服務器將其視為空字典,即使發送時其中包含值。
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

當您將帖子作為 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...


查看完整回答
反對 回復 2022-12-09
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號