不允许的方法请求的方法不允许 URL
Method Not Allowed The method is not allowed for the requested URL
我正在尝试 return 单击后的随机数列表,但出现错误消息“方法不允许该方法不允许用于请求的 URL。”
我对烧瓶真的很陌生。
以下是代码:
main.py
import flask
from flask import Flask, render_template, request, jsonify
import numpy as np
app = Flask(__name__)
p=[]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/rannum/', methods=['POST'])
def rannum():
print("clicked")
p = []
p = np.random.randint(100, size=10)
for i in p:
print(i)
return jsonify(p)
if __name__ == '__main__':
app.run(debug=True)
index.html
这是 HTML 文档。
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Flask App</title>
<style type="text/css">
* {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>random number generation</h1>
<form method="post" id="form">
<label >click button</label>
<button>click</button>
</form>
<p id="rannum"></p>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$.ajax({
url: "{{ url_for( 'rannum' ) }}",
type: "POST",
data: nameArray,
success: function( resp ){
console.log( resp )
}
})
</script>
</body>
</html>
只需将您的代码更改为:
@app.route('/rannum/', methods=['POST'])
def rannum():
print("clicked")
p = []
p = np.random.randint(100, size=10)
for i in p:
print(i)
return jsonify({"data": p})
注意:不能将列表作为响应提供给用户,请注意您不能对列表使用jsonify
功能。当你有 dictionary
.
时使用 jsonify
我正在尝试 return 单击后的随机数列表,但出现错误消息“方法不允许该方法不允许用于请求的 URL。” 我对烧瓶真的很陌生。 以下是代码:
main.py
import flask
from flask import Flask, render_template, request, jsonify
import numpy as np
app = Flask(__name__)
p=[]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/rannum/', methods=['POST'])
def rannum():
print("clicked")
p = []
p = np.random.randint(100, size=10)
for i in p:
print(i)
return jsonify(p)
if __name__ == '__main__':
app.run(debug=True)
index.html 这是 HTML 文档。
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Flask App</title>
<style type="text/css">
* {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>random number generation</h1>
<form method="post" id="form">
<label >click button</label>
<button>click</button>
</form>
<p id="rannum"></p>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$.ajax({
url: "{{ url_for( 'rannum' ) }}",
type: "POST",
data: nameArray,
success: function( resp ){
console.log( resp )
}
})
</script>
</body>
</html>
只需将您的代码更改为:
@app.route('/rannum/', methods=['POST'])
def rannum():
print("clicked")
p = []
p = np.random.randint(100, size=10)
for i in p:
print(i)
return jsonify({"data": p})
注意:不能将列表作为响应提供给用户,请注意您不能对列表使用jsonify
功能。当你有 dictionary
.