[英]how to get result from mysql and display using jquery when radio button is clicked?
[英]Using info from GET request in jQuery to display transformed result through Flask
我正在嘗試根據客戶做出的選擇來發送客戶信息。 我想使用GET請求,因為我沒有更改服務器上的任何信息。 但是,我不知道如何從GET請求中實際訪問任何信息。 我一直在研究將查詢字符串與Flask一起使用,但是運氣不高。 返回到javascript之后,我需要對結果進行額外的操作,因此我希望將響應保留在成功函數中,因為下面有它,而不是使用任何模板。
我真正有能力改變的唯一事情就是如何發送數據(如果它需要是JSON或除字符串之外的其他東西)以及如何在Flask中進行訪問。 那有可能嗎,我會怎么做?
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
def myFunc(s):
return str(s) + "!"
@app.route("/")
def index():
# resp = myFunc(selectedOption)
# return resp
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html>
<head>
<title>getTest</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="{{ url_for('static', filename='script.js')}}"></script>
</head>
<body>
<select id="options">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
</select>
<button class="btn btn-default" id="submit">Submit</button>
<p id="demo"></p>
</body>
</html>
$(document).ready(function() {
$("#submit").click(function() {
var selectedOption = $("#options").val();
$.ajax({
url: "/",
method: "GET",
data: selectedOption, //open to changing
success: function(result) {
$("#demo").html(result);
}
});
});
});
您應該更改ajax調用,以便它使用命名參數,而不僅僅是將數據設置為與您的選項相等
$.ajax({
url: "/",
method: "GET",
data: {'selectedOption': selectedOption}, //open to changing
success: function(result) {
$("#demo").html(result);
}
});
使用request.args.get('selectedOption')
訪問查詢字符串
@app.route("/")
def index():
resp = myFunc(request.args.get('selectedOption'))
return resp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.