ラズベリーパイ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が適用されない:
- Sending POST Data through Flask
- POST 405 (Method Not Allowed) Method Not Allowed flask error 405
- method not allowed error in flask
- Flask - POST Error 405 Method Not Allowed
- 。
あなたのルートが '/'のときになぜ '/static/wificonfig.py'に投稿していますか? –
これはスクリプトがある場所で、Flaskはファイル(画像など)を置く場所ですから。さて、スクリプトを他の場所に置く必要がある場合、私は学びたいと思っています。それが私の問題だと思いますか? –
@Daniel Rosemanスクリプトを/ home/pi/Elithion /に移動して、その行をvarに変えるので、WifiConfigScript = '/wificonfig.py';私は404を取得します。私はまたvar WifiConfigScript = 'wificonfig.py'を試しました。とvar WifiConfigScript = 'Elithion/wificonfig.py'; –