繁体   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