2017-09-22 6 views
0

ラズベリーパイFlaskはWebページを提供し、JavaScriptを使用してPythonスクリプトに投稿します。"GET"はスクリプトのテキストを取得し、 "POST"の結果は "405メソッドが許可されていません"、I _did_ methods = ['GET'、POST '](FlaskからPythonへのJavaScriptから)

フォルダ構造:

/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 - 

私は「GET」に「POST」を変更した場合、それはスクリプト内のテキストを返しますので、私はパスが正しいことを知っています。

私はこれらのStackOverflow答えをチェックし、彼らが助けていない、私は正しい道を持っているので、私はHTMLフォームを使用していない、とCORSが適用されない:

+2

あなたのルートが '/'のときになぜ '/static/wificonfig.py'に投稿していますか? –

+0

これはスクリプトがある場所で、Flaskはファイル(画像など)を置く場所ですから。さて、スクリプトを他の場所に置く必要がある場合、私は学びたいと思っています。それが私の問題だと思いますか? –

+0

@Daniel Rosemanスクリプトを/ home/pi/Elithion /に移動して、その行をvarに変えるので、WifiConfigScript = '/wificonfig.py';私は404を取得します。私はまたvar WifiConfigScript = 'wificonfig.py'を試しました。とvar WifiConfigScript = 'Elithion/wificonfig.py'; –

答えて

1

デヴィッド主義の説明のおかげで、それを解決しました。

フォルダ構造:

/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ではなく、このルートに投稿されます。 url_forでURLを生成し、JavaScriptの値を使用しているので、tojsonを使用してください。

var wifiConfigScript = {{ url_for('wificonfig')|tojson }}; 
関連する問題