我试图迭代数组以在Div
中创建span元素JS
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function (e) {
var url = "{{ url_for('ajaxPP') }}"; // send the form data here.
$.ajax({
type: "POST",
url: url,
data: $('form').serialize(), // serializes the form's elements.
dataType:'json',
success: function (data) {
$('#result').html("<br>"+data.result);
$('input[name="search-input"]').val("");
$('input[name="search-input"]').focus();
console.log(data) // display the returned data in the console.
}
});
e.preventDefault(); // block the traditional submission of the form.
});
});
</script>
尝试使用:data.result [0],data [0],result [0] 访问,它无法正常工作
控制台响应
{result: Array(1)}result: Array(1)0: Array(6)0: "Test1"1: "Test2"2: "Test3"3: 118804: "Test5"5: nulllength: 6__proto__: Array(0)length: 1__proto__: Array(0)__proto__: Object
app.py
@app.route('/ajaxPP',methods=['POST'])
def ajaxPP():
if request.method == 'POST':
us = request.form['search-input']
user = getUser(us)
return jsonify(result=user)
答案 0 :(得分:0)
从您的服务器返回的数据未正确形成JSON。您需要将您的回复包装在Flask的'jsonify'方法中。
答案 1 :(得分:0)
我使用以下方法迭代数组:
$('#result').html("<br>"+data.result[0][0]);