Answer the question
In order to leave comments, you need to log in
Why is flask request not working?
Hello. I have a page that sends a request with Ajax, when I try to get something with request it gives an error
AttributeError: 'function' object has no attribute '...'
import logging
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
"""Return a friendly HTTP greeting."""
return """
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: '/request',
type: 'post',
contentType: "application/json; charset=utf-8",
data: {"test":"5"},
success: function (data) {
console.log(data);
}
});
});
</script>
"""
@app.route('/request', methods=['POST'])
def request():
return request.method # здесь выдает ошибку
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace.
""".format(e), 500
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]
Traceback (most recent call last):
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\Other\Python3\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "F:\Other\Python3\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "F:\Other\Python3\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:\Python projects\flask\main.py", line 45, in request
return request.method # здесь выдает ошибку
AttributeError: 'function' object has no attribute 'method'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question