簡體   English   中英

所請求的網址不允許使用該方法

[英]The method is not allowed for the requested URL

通過Facebook Messenger進行訪問時出現此錯誤?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

這是代碼

網絡應用

from flask import Flask
from flask import request
from flask import render_template, redirect, url_for, request,jsonify,
app = Flask(__name__, template_folder='./')
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
    if request.method == 'POST':
        if request.form != None and 'message' in request.form:
            msg = request.form['message']
            response =  pred(str(msg))
            return jsonify(response) 
    else:
        return render_template('index.html')

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

index.html

<html>
<head>
    <title>Upload</title>
</head>
<body>
    <p1>
        Yooooo Tensorflow thoooo
    </p1>
<form action="{{ url_for('prediction') }}" method='POST' >
    <input type=text name="message" value="" maxlength="100">
    <input type="submit" value="submit" >
</form>
</body>
</html>

我試圖通過我使用heroku連接的Facebook Messenger運行

由於您的視圖中同時啟用了POST和GET,因此很可能是CORS錯誤

請按照此處的說明進行操作以允許針對Apache的跨源請求

或者如果您的Web服務器是nginx,則在此處

例如,我使用apache托管在ubuntu 16.04上的應用程序,我運行

sudo nano /etc/apache2/sites-available/000-default.conf

在終端上

我編輯服務器配置,並添加標頭集Access-Control-Allow-Origin“ *”以允許跨源請求。 星號是通配符,表示我允許來自任何網站的請求,您可以將其替換為要允許的網站

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    Header set Access-Control-Allow-Origin "*"
    #Include conf-available/serve-cgi-bin.conf
    WSGIScriptAlias / /var/www/html/myapp.wsgi
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mysite.com [OR]
RewriteCond %{SERVER_NAME} =mysite.com.info
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

忽略將請求重定向到我的應用程序到https的重寫規則。 您只需要添加標題集Access-Control-Allow-Origin“ *”

暫無
暫無

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

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