[英]How to send data from jquery to flask
我使用的數據表 ,如下圖所示,以使數據分頁表中。 我添加了一個額外的功能,使它可以在單擊時從行中獲取值( 請參閱此 )
</head>
<body>
<div>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on('click', 'tr', function () {
var data = table.row( this ).data();
alert( 'You clicked on '+data[0]+'\'s row' );
} );
} );
</script>
</body>
</html>
如何使用Ajax調用將data[0]
中的該值傳遞給flask函數process
這樣我就不必刷新整個頁面,即, process()
刷新指定的DOM? 我是新手,請多多包涵。
from flask import Flask, render_template, request
app = Flask(__name__)
app.debug=True
@app.route('/')
def index(name=None):
return render_template('index.html', name=name)
@app.route('/process', methods=["GET", "POST"])
def process():
#Do something with the value
return render_template('process.html', value=value)
if __name__ == '__main__':
app.run()
使用flask_restful創建Flask API服務器,並從jQuery腳本接收json作為帖子。
以下應該讓您了解我的意思:
from flask_restful import Resource, Api
from flask import Flask, request
import json
app = Flask(__name__)
api = Api(app)
class DataHandler(Resource):
def get(self):
return stuff
def post(self):
try:
args = request.json
# handle stuff
return 200
except:
return 400
api.add_resource(DataHandler, '/endpoint_name')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.