2017-09-22 67 views
0

在Raspberry Pi中,我使用Flask提供一個網頁,該網頁使用JavaScript發佈到Python腳本。「GET」獲取腳本文本,「POST」導致「405方法不允許」,我_did_ methods = ['GET','POST'](從JavaScript通過Flask到Python)

文件夾結構:

/home/pi/Elithion/app.py 
/home/pi/Elithion/templates/index.html 
/home/pi/Elithion/static/wificonfig.py 

app.py(瓶使用Python代碼)

from flask import Flask, render_template, url_for 

app = Flask(__name__) 

@app.route('/', methods=['GET', 'POST']) 
def index(): 
    return render_template('index.html') 

if __name__ == '__main__': 
    app.run(debug=True, host='0.0.0.0') 

的index.html,JavaScript的:

function ReqWifiConfig(selectedWiFiNetwork, wiFiPassword) { // Request setting the WiFi configuration 

    // Constants 
    var WifiConfigScript = '/static/wificonfig.py'; 
    var ContentKey = 'Content-type'; 
    var ContentVal = 'application/x-www-form-urlencoded'; 

    // Send the wifi login credentials to the Python script using AJAX 
    var xmlhttp = new XMLHttpRequest();  
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      ... 
     } 
     else if (xmlhttp.readyState==4) { 
      alert(xmlhttp.status + xmlhttp.statusText); 
     } 
    } 
    xmlhttp.open("POST", WifiConfigScript, true); 
    xmlhttp.setRequestHeader(ContentKey, ContentVal); 
    var postData = 'nw=' + selectedWiFiNetwork + '&pw=' + wiFiPassword; 
    xmlhttp.send(postData);  
} 

這導致該警報的瀏覽器:

127.0.0.1:5000 says: 405METHOD NOT ALLOWED 

,這消息在終端:

127.0.0.1 - - (date) "POST/static/wificonfig.py HTTP/1.1 405 - 

如果我改變「POST」到「GET」,則返回在腳本中的文本,所以我知道的路徑是正確的。

我檢查這些StackOverflow的答案,他們沒有幫助,因爲我有正確的道路,我沒有使用HTML表單,和CORS不適:

+2

當你的路由只是'/'時,你爲什麼要發佈到'/static/wificonfig.py'? –

+0

因爲這就是腳本的位置,這就是Flask希望你放置文件(圖像等)的地方。現在,如果我應該把腳本放在其他地方,我很想學習。你認爲這是我的問題嗎? –

+0

@Daniel Roseman,因爲如果我將腳本移動到/ home/pi/Elithion /,並將該行更改爲WifiConfigScript ='/wificonfig.py';我得到了404。我也嘗試過var WifiConfigScript ='wificonfig.py';和var WifiConfigScript ='Elithion/wificonfig.py'; –

回答

1

解決它,這要歸功於davidism的澄清。

文件夾結構:

/home/pi/Elithion/app.py 
/home/pi/Elithion/templates/index.html 
/home/pi/Elithion/wificonfig.py 

app.py(使用燒瓶Python代碼)

from flask import Flask, render_template, request 
import wificonfig 

app = Flask(__name__) 

# Show the HTML page 
@app.route('/') 
def index(): 
    return render_template('index.html') 

# Service the POST request 
@app.route('/postService', methods=['POST']) 
def postService(): 
    wifiStatus = 'fail' 
    if request.method == 'POST': 
     nw = request.form['nw'] # WiFi network 
     pw = request.form['pw'] # WiFi Password 
     wifiStatus = wificonfig.configwifi(nw, pw) 
    return wifiStatus 

if __name__ == '__main__': 
    app.run(debug=True, host='0.0.0.0') 

wificonfig.py腳本:

def configwifi(nw, np): 
    """ Sign onto the WiFi specified network with the given password """ 
    # ... Code to sign onto the WiFi network 
    return 'OK' 

的index.html,JavaScript的:

function ReqWifiConfig(selectedWiFiNetwork, wiFiPassword) { // Request setting the WiFi configuration 

    // Constants 
    var WifiConfigScript = '/postService'; 
    var ContentKey = 'Content-type'; 
    var ContentVal = 'application/x-www-form-urlencoded'; 

    // Send the login credentials to the Python script using AJAX 
    var xmlhttp = new XMLHttpRequest();  
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      alert(xmlhttp.status + xmlhttp.statusText); // Returns 'OK' 
     } 
    } 
    xmlhttp.open("POST", WifiConfigScript, true); 
    xmlhttp.setRequestHeader(ContentKey, ContentVal); 
    var postData = 'nw=' + selectedWiFiNetwork + '&pw=' + wiFiPassword; 
    xmlhttp.send(postData);  
} 
3

您無法發佈到靜態文件。如果你想運行Python代碼,你可以在Flask視圖函數中做到這一點。將您的腳本移到您的應用程序旁邊,以便它是可導入的,導入它,調用它並返回響應。

from flask import request, jsonify 
from import wificonfig import do_config 

@app.route('/wificonfig', methods=['POST']) 
def wificonfig(): 
    result = do_config(nw=request.form['nw'], pw=request.form['pw']) 
    return jsonify(result) 

模板中的JavaScript張貼到此路線,而不是wificonfig.py。由於您使用的是JavaScript中的值,因此請使用url_for生成網址並使用tojson

var wifiConfigScript = {{ url_for('wificonfig')|tojson }}; 
相關問題