请求的URL不允许使用该方法

时间:2018-04-21 19:30:15

标签: python flask facebook-messenger-bot

通过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>

这是代码

web应用

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;

1 个答案:

答案 0 :(得分:0)

由于您的视图中启用了POST和GET,因此很可能是CORS错误

按照here说明允许apache的跨域请求

如果您的Web服务器是nginx,则为here

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

sudo nano /etc/apache2/sites-available/000-default.conf
终端上的

我编辑我的服务器配置并添加标头设置Access-Control-Allow-Origin&#34; *&#34; 以允许跨源请求。明星是一张外卡,意味着我允许来自任何网站的请求,您可以将其替换为您想要允许的网站

<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&#34; *&#34;