Python 如何从flask中的ajax帖子执行ajax GET
我想在按钮列表上执行两种ajax方法,Python 如何从flask中的ajax帖子执行ajax GET,python,ajax,flask,Python,Ajax,Flask,我想在按钮列表上执行两种ajax方法,POST和GET。单击的按钮保存值index,并应将其传递给由ajax GET触发的方法 我能够执行调用'/download\u file'的GET部分,但是我还不能创建一个可以将索引值传递给方法的ajaxPOST 这里是我在app.py中的函数(简化): 和我的html: <body> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&
POST
和GET
。单击的按钮保存值index
,并应将其传递给由ajax GET触发的方法
我能够执行调用'/download\u file'
的GET
部分,但是我还不能创建一个可以将索引
值传递给方法的ajaxPOST
这里是我在app.py中的函数(简化):
和我的html:
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#test').bind('click', function() {
$.getJSON('/download_file',
function(data) {
//do nothing
});
return false;
});
});
</script>
<div>
<h1>messages list</h1>
<div>
<ol>
{%for index, message in enumerate(messages)%}
<li>{{ index }}{{ message.date }}-{{message.name}}</li>
<form>
<a href="" id=test><button id="btn" value="{{ index }}" class='btn btn-default'>Download</button></a>
</form>
{%endfor%}
</ol>
</div>
</div>
</body>
$(函数(){
$('a#test').bind('click',function()){
$.getJSON(“/download_file”,
功能(数据){
//无所事事
});
返回false;
});
});
消息列表
{%用于索引,枚举中的消息(消息)%}
{{index}}{{message.date}}-{{message.name}
{%endfor%}
首先:您不需要
。您可以将单击
绑定到
您不必发送GET
。您可以从按钮获取索引
,并使用$.POST()
将其发送到POST
最小工作示例
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/')
def index():
data = [
{'date': 'dateA', 'name': 'nameA'},
{'date': 'dateB', 'name': 'nameB'}
]
return render_template_string('''
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('button').bind('click', function() {
value = $(this).attr('value');
$.post('/download_file', //?index=' + value,
{'index': value},
function(data) {
alert("Response: " + data);
});
return false;
});
});
</script>
<div>
<h1>messages list</h1>
<div>
<ol>
{%for message in messages %}
<li>{{ message.date }} - {{message.name}}</li>
<form>
<button id="btn" value="{{ loop.index0 }}" class='btn btn-default'>Download</button></a>
</form>
{%endfor%}
</ol>
</div>
</div>
</body>''', messages=data)
@app.route('/download_file', methods=['GET', 'POST'])
def save_doc():
print('method:', request.method)
index = request.form.get('index')
print('form["index"]:', index)
#print('args:', request.args)
#print('form:', request.form)
#print('data', request.data)
#print('files', request.files)
return "Server: " + str(index)
app.run() #debug=True
从烧瓶导入烧瓶,请求,呈现\u模板\u字符串
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
数据=[
{'date':'dateA','name':'nameA'},
{'date':'dateB','name':'nameB'}
]
返回渲染模板字符串(“”)
$(函数(){
$('button').bind('click',function(){
value=$(this.attr('value');
$.post('/download_file',//?index='+值,
{'index':值},
功能(数据){
警报(“响应:+数据);
});
返回false;
});
});
消息列表
{消息%中的消息为%s}
{{message.date}-{{message.name}
下载
{%endfor%}
'',消息=数据)
@app.route('/download_file',methods=['GET','POST'])
def save_doc():
打印('method:',request.method)
index=request.form.get('index')
打印('表格[“索引]:',索引)
#打印('args:',request.args)
#打印('form:',request.form)
#打印('data',request.data)
#打印('files',request.files)
返回“服务器:”+str(索引)
app.run()#debug=True
为什么不一次发送所有请求-GET
或POST
?IBTW:要接收POST
您需要@app.route('/download_file',methods=['GET',POST'])
。默认情况下,route
只接受GET
请求。var index=$('a#test').attr(“value”)
?在函数内部,必须使用request.form.GET(“variable”)
(如果在正文中发送variable
)或request.args.GET(“variable”)
(如果以url?variable=…
的形式发送)我对flask很陌生,所以这有点让人困惑,但我如何在一个请求中发送所有内容呢?我已经更新了我的代码谢谢,这几乎解决了所有问题。最后一个问题,data
参数来自函数(data){alert(“Response:+data”);}的内部代码>它是flask作为响应返回”服务器发送的数据:“+str(index)
-jQuery将其分配给变量数据
,该变量在函数(数据){…}
$('button').bind('click', function() {
value = $(this).attr('value');
$.post('/download_file'
{'index': value},
function(data) {
//do nothing
});
return false;
});
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/')
def index():
data = [
{'date': 'dateA', 'name': 'nameA'},
{'date': 'dateB', 'name': 'nameB'}
]
return render_template_string('''
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('button').bind('click', function() {
value = $(this).attr('value');
$.post('/download_file', //?index=' + value,
{'index': value},
function(data) {
alert("Response: " + data);
});
return false;
});
});
</script>
<div>
<h1>messages list</h1>
<div>
<ol>
{%for message in messages %}
<li>{{ message.date }} - {{message.name}}</li>
<form>
<button id="btn" value="{{ loop.index0 }}" class='btn btn-default'>Download</button></a>
</form>
{%endfor%}
</ol>
</div>
</div>
</body>''', messages=data)
@app.route('/download_file', methods=['GET', 'POST'])
def save_doc():
print('method:', request.method)
index = request.form.get('index')
print('form["index"]:', index)
#print('args:', request.args)
#print('form:', request.form)
#print('data', request.data)
#print('files', request.files)
return "Server: " + str(index)
app.run() #debug=True