簡體   English   中英

使用XMLHttpRequest的AJAX請求返回本地Python文件的內容,而不是執行它

[英]AJAX request using XMLHttpRequest returns content of local Python file instead of executing it

網頁和Python腳本在Raspberry Pi中都是本地的(沒有Internet連接)。

從命令行調用Python腳本可以正常運行。

網頁中的JavaScript使用AJAX調用Python腳本:

    var xmlhttp = new XMLHttpRequest();     
    xmlhttp.onreadystatechange=function() {
        alert('readyState: '+ xmlhttp.readyState)     // Returns: 4
        alert('status: '+ xmlhttp.status)             // Returns: 0
        alert('statusText: '+ xmlhttp.statusText)     // Returns: (Blank)
        alert('responseText: '+ xmlhttp.responseText) // Returns contents of script
    }
    xmlhttp.open("POST", "myscript.py", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send('test');   

我也嘗試過“ GET”,結果相同:

    var xmlhttp = new XMLHttpRequest();     
    xmlhttp.onreadystatechange=function() {
        alert('readyState: '+ xmlhttp.readyState)     // Returns: 4
        alert('status: '+ xmlhttp.status)             // Returns: 0
        alert('statusText: '+ xmlhttp.statusText)     // Returns: (Blank)
        alert('responseText: '+ xmlhttp.responseText) // Returns contents of script
    }
    xmlhttp.open("GET", "myscript.py", true);
    xmlhttp.send();     

該腳本未執行(我可以從日志中看出)。

我是否誤解了AJAX可以做什么? 它不能運行本地腳本嗎?

我的最終目標是:將用戶條目從網頁傳遞到腳本,以配置Raspberry Pi。

編輯:同樣,一切都是本地的:沒有服務器,沒有Internet,沒有HTTP。

如果答案是“不,沒有服務器,您將無法做到”,這就是我所需要聽到的,我將考慮使用端口80來實現我想要的功能。

Python(響應HTTP請求)由Web服務器執行。

如果從file:// URL獲取,則將顯示錯誤或原始內容,具體取決於您的瀏覽器。

您可以在任何端口上運行可以調用Python腳本(使用Python或CGI變體)的任何HTTP服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM