我正在嘗試將從getTableData()函式(在 ajax 請求內部)推送的songFiles陣列發送到/api/fileNames端點,然后將其發送到應該發布陣列的回呼postFileNames(),但我只是可以t 似乎讓它作業。任何幫助將不勝感激,可能包括其他方法。
我是使用 ajax 和燒瓶的初學者,我已經四處尋找解決方案,但似乎找不到任何適合我的目標,請原諒我缺乏知識。
JavaScript 代碼
function getTableData() {
$.ajax({
method: "GET",
url: "/api/getSong",
dataType: "json",
success: function(data) {
createMusicTable();
let indexObj = Object.keys(data.song).length;
for (var i = 0; i < indexObj; i ) {
var song = data.song[i]
var id = data.song[i].song_id;
var fileName = data.song[i].song_file ".mp3";
songFiles.push(fileName);
appendMusic(song, id);
songTitle.id = "s" i;
console.log("td-ok");
}
postFileNames(songFiles);
}
});
}
function postFileNames(data) {
$.ajax({
method: "POST",
url: "/api/fileNames",
dataType: "json", // data is an array, so not sure what to put here.
data: data, // not sure if correct
success: function(data) {
// should: post the data into the endpoint.
// data is not logged, goes straight to error.
console.log(data)
},
error: function() {
console.log("cry")
}
})
}
燒瓶端點代碼
@app.route("/api/fileNames", methods=['POST', 'GET'])
def fileNameStorage():
if request.method == 'POST':
data = request.get_data() # not too sure what to do here either.
return data
else:
return "error" # currently goes to 'return error'
uj5u.com熱心網友回復:
@app.route("/api/fileNames", methods=['POST', 'GET'])
def fileNameStorage():
if request.method == 'POST':
data = {
"id": request.json["id"],
"song_name": request.json["song_name"],
"time_duration": request.json["time_duration"]
}
return data, 200
else:
return "error", 400 # currently goes to 'return error'
如果你愿意的話會更漂亮:
@app.route("/songs/", methods=['GET', 'POST'])
def songs():
return Songs().get_songs(), 200
在 routes.py 檔案和另一個檔案中的其他類和方法
如果映射陣列元素有問題,可以嘗試 jsonify(array) 或 json.dump(array) 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/471045.html
標籤:javascript 数组 阿贾克斯 烧瓶 邮政
上一篇:重新加載不會改變POST狀態
