以下是我的观点:
import db_connect_test
from db_connect_test import Viz_Connector
from flask import Flask, request, session, redirect, url_for, render_template, flash,json,jsonify
import os
app = Flask(__name__)
@app.route('/',methods = ['GET','POST'])
def select_ID():
if request.method == 'POST':
ID=request.form['input_ID']
Node_type = request.form['Node_Type']
while True:
try:
data = Viz_Connector(ID,Node_type).get_data()
return render_template('dropdown.html',data=jsonify(data))
break
except IndexError:
return "Wrong ID"
return render_template('dropdown.html')
下面是我的jquery / ajax。我正在使用它,因为我的HTML中有一个下拉列表:
所有jquery代码都保存在名为“final.js”的文件中,并在下面的HTML中引用。
$('input[type=submit]').click(function() {
var input_ID = $("#input_ID").val();
var Node_Type = $("#Node_Type").val();
$.ajax({
type: "POST",
url: "/",
dataType: 'json',
data: {
input_ID: input_ID,
Node_Type: Node_Type,
},
success: function(data) {
console.log(data);
}
});
return false;
});
以下是输入ID和ID_type的HTML:
<form action="{{url_for('select_ID')}}" method="POST">
<label for="input_ID">ID</label>
<input id="input_ID" type="text" />
<label for="Node_Type">Node_Type</label>
<select id="Node_Type">
<option value=Customer>Customer</option>
<option value=Phone>Phone</option>
<option value=ID_Card>ID_Card</option>
</select>
<input type='submit' value='Submit'>
<script src="{{url_for('static',filename='final.js')}}"></script>
</form>
下拉列表用于ID_Type并具有3个可能的值
"Customer","Phone" and "ID_Card".
“ID”是一个文本字段,其值为
"C1","C2","C3","C4","C5","C6" for "Customer"
"ID1", "ID2" ,"ID3","ID4" for ID_Card
"P1" ,"P2" for phone.
后端服务器是neo4j数据库服务器。
进入
"C1" as ID and ID_type as "Customer"
以下数据已生成:
在运行烧瓶视图时,我获得了一个成功的POST:200。但是
console.log(data) seems to display the entire HTML page and **NOT** the data from the server.
答案 0 :(得分:0)
尝试使用Flask的jsonify()
函数传递数据:
return jsonify(data=data)
文档:AJAX with jQuery。
编辑$ .ajax方法:
...
success: function(data) {
console.log(data);
}
...
答案 1 :(得分:0)
所以,问题是我还要返回渲染模板('dropdown.html')。
修改烧瓶代码如下:
import db_connect_test
from db_connect_test import Viz_Connector
from flask import Flask, request, session, redirect, url_for, render_template, flash,json,jsonify
import os
app = Flask(__name__)
@app.route('/',methods = ['GET','POST'])
def select_ID():
if request.method == 'POST':
ID=request.form['input_ID']
Node_type = request.form['Node_Type']
while True:
try:
data = Viz_Connector(ID,Node_type).get_data()
return json.dumps(data)//no rendering of template
break
except IndexError:
return "Wrong ID"
return render_template('dropdown.html')