0
我想在這裏執行一個非常簡單的任務...在HTML頁面上添加兩個數字。結果必須來自一個python腳本。這必須使用AJAX完成。因此,我已創建了下面的頁面與javscript的Ajax調用和:使用AJAX和python腳本添加兩個數字
<!DOCTYPE html>
<html lang="en">
<head>
<script>
var req = new XMLHttpRequest();
function askServer() {
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
var dataToSend = "?number1="+number1 + "&number2="+number2;
req.open("GET","myScript.py",dataToSend,true);
req.onreadystatechange = handleServerResponse;
req.send();
document.getElementById("result").innerHTML = "The request has been sent.";
}
function handleServerResponse() {
if((req.readyState == 4) && (req.status == 200)) {
var result = req.responseText;
document.getElementById("result").innerHTML = "The answer is : " + result + ".";
}
}
</script>
</head>
<body>
<p>Enter two numbers:
<input id="number1" type="number">
<input id="number2" type="number">
</p>
<p>To ask the server top add them, without refreshing the page, click this button: <button onclick="askServer()">Ask the Server</button></p>
</div>
<p id="result"></p>
</body>
</html>
我需要一些幫助的Python腳本。我知道它只需要查詢字符串中的兩個參數;添加並返回。
說我已經創建了下面的語句
return "Hi".
完整文本「迴歸‘HI!’」返回的結果.py文件。我只是一個Python初學者。
您使用哪種服務器設置? – Daniel