简体   繁体   中英

The method is not allowed for the requested URL

Getting this error when I am accessing through 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>

Here is code

webapp

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>

I am trying to run through Facebook messenger which I am connecting using heroku

Since both POST and GET are enabled in your view, this is most likely a CORS error

Follow the instructions here to allow cross origin requests for apache

Or here if your Web server is nginx

For example, my apps hosted on ubuntu 16.04 using apache, i run

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

on the terminal

I edit my servers configuration and add Header set Access-Control-Allow-Origin "*" to allow cross origin requests. The star is a wild card meaning I allow requests from any site, you can replace it with the sites you want to allow

<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

Ignore the rewrite rules that redirects requests to my app to https. You only need to add Header set Access-Control-Allow-Origin "*"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM