有使用AJAX的请求。我正在向服务器发送数据并获得响应。这没有问题。但是:
alert(response)返回[object Object]。
alert(response.data [0])返回undefined。
function requestFromCalendar() {
var first = selected[0];
var second = selected[1];
first.month = first.month+1;
second.month = second.month+1;
$.ajax({
data: {
first: JSON.stringify(first),
second: JSON.stringify(second)
},
type: 'POST',
dataType: 'json',
url: '/get_selected_values'
}).done(function(response) {
alert(response);
alert(response.data[0]);
})
}
@app.route('/get_selected_values', methods=['POST'])
def get_selected_values():
values = request.form.get("first")
first_date = json.loads(values)
values = request.form.get("second")
second_date = json.loads(values)
t = time(0, 0, 0, 0)
d = date(year=first_date['year'], day=first_date['day'], month=first_date['month'])
start_date = datetime.combine(d, t)
start_date = start_date + timedelta(microseconds=123456)
d = date(year=second_date['year'], day=second_date['day'], month=second_date['month'])
end_date = datetime.combine(d, t)
end_date = end_date + timedelta(microseconds=123432)
data = fetch_selected_date(start_date=start_date, end_date=end_date, login=current_user.get_id())
if data:
return jsonify({'data': data})
return jsonify({'data': []})
什么事?怎么解决?
编辑1
console.log(response)返回{data:Array(0)}
console.log(response.data [0])返回undefined
编辑2
发送数据存储在python代码中。在调试模式下可以看到,所以问题肯定不存在