0
我對web開發相對比較陌生,並且試圖讓客戶端的javascript將GET請求發送到在服務器上運行的python腳本,並根據該請求返回數據。我試圖調整我在網上找到的webpy庫的例子無濟於事。無論何時發送GET請求,XMLHttpRequest()的responseText屬性都會返回python文件的文本而不是數據。任何意見將不勝感激!與AJAX一起使用webpy
JavaScript函數:
function sendSerialCommand(selection, command) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
if (command !== 5) {
document.getElementById("output2").innerHTML = xmlhttp.responseText;
document.getElementById("output2").style.color = "green";
} else {
document.getElementById("output1").innerHTML = xmlhttp.responseText;
console.log(xmlhttp.responseText);
document.getElementById("output1").style.color = "green";
}
}
};
xmlhttp.open("GET", pythonFileName + "?sel=" + selection + "?cmd=" + command, true);
xmlhttp.send();
}
...和測試Python腳本:
import web
urls = (
'/', 'Index'
)
app = web.application(urls,globals())
#MAIN LOOP
class Index:
def GET(self):
webInput = web.input()
return 'message: GET OK!'
if __name__ == "__main__":
app.run()
您應該查看[Django](https://www.djangoproject.com/)和他們的[Rest API](http://www.django-rest-framework.org/)。另外,您可以使用[Jquery](http://jquery.com/)輕鬆選擇您的元素。 – k4yaman