我正在嘗試將一組模塊名稱和一組標記發送到在 python 燒瓶服務器上運行的函式。
function getClassification()
{
const modulesArray = [
document.getElementById('module_1').value,
document.getElementById('module_2').value,
document.getElementById('module_3').value,
document.getElementById('module_4').value,
document.getElementById('module_5').value
]
const marksArray = [
document.getElementById('mark_1').value,
document.getElementById('mark_2').value,
document.getElementById('mark_3').value,
document.getElementById('mark_4').value,
document.getElementById('mark_5').value
]
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var j = JSON.parse(this.response);
}
};
xhttp.open("GET",classifyModulesURL "?modules=" modulesArray "&marks=" marksArray);
xhttp.send();
return;
}
當燒瓶服務器收到資訊時,它被視為一個字串。
modules = request.args.get('modules')
print(modules)
marks = request.args.get('marks')
print(marks)
print(type(marks))
輸出
CSC3021,CSC4302,CSC3299,CSC5678,CSC7623
45,66,43,54,78
<class 'str'>
我可以在 python 端進行字串操作以將字串轉換為串列,但我認為發送 javascript 陣列會保留結構。我做錯了什么嗎?
謝謝
modules = request.args.get('modules')
print(modules)
marks = request.args.get('marks')
print(marks)
print(type(marks))
輸出
CSC3021,CSC4302,CSC3299,CSC5678,CSC7623
45,66,43,54,78
<class 'str'>
uj5u.com熱心網友回復:
這是因為您將資料作為查詢引數(www.example.com?arg1=val1&arg2=val2)發送,因為它們是 url 的一部分,所以只能作為字串發送。要保留串列,您應該創建一個 json 物件以在POST請求的正文中發送。同時,必須在服務器端正確處理請求,即您將不再需要訪問 http 請求引數,而是訪問其 json 內容。
看看如何用燒瓶處理 json 請求。
看看如何使用 XMLHttpRequest發布 json
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/529866.html
