重新创建一个页面以使用django表单(当前屏幕在呈现模板时不使用任何django窗体实现(,以从谷歌自动完成位置接收数据。基本上只是从谷歌获取响应,将其转换为json,并使用表单数据和谷歌数据进行ajax调用。
我遇到的问题是,当我序列化表单以将数据传递到ajax调用时,存储该json值的字段将以json形式封装在数组中。查看当前页面,我发现这并没有发生,从谷歌的api中检索到的数据也是正确的。
处理从谷歌获取数据的javascript是相同的,当将其应用于html中的谷歌详细信息字段时,通过对从谷歌检索的数据执行.val(JSON.stringify(((来更新值。
<form id="myForm">
<input type="hidden" id="google-details" name="google-details">
{{ form }}
<button type="submit" id="submit">submit
</form>
<script type="text/javascript">
$(document).ready(function () {
$("#myForm").on("submit", function () {
event.preventDefault();
do_ajax_call();
}
}
function do_ajax_call() {
let foo = $("#myForm").serialize();
$.ajax({
url: '/destination/',
type: 'post',
data: foo,
success: function(res) {
//Do stuff
}
})
}
</script>
我从更新页面上的ajax获得的数据
['{"address_components":[{"long_name":"20","short_name":"20","types":["street_number"]},{"long_name":"West 34th Street","short_name":"W 34th St.","types":["route"]},{"long_name":"Manhattan","short_name":"Manhattan","types":["sublocality_level_1","sublocality","political"]},{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"New York County","short_name":"New York County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"10001","short_name":"10001","types":["postal_code"]}],"business_status":"OPERATIONAL","formatted_address":"20 W 34th St., New York, NY 10001, USA","name":"Empire State Building","place_id":"ChIJaXQRs6lZwokRY6EFpJnhNNE","types":["tourist_attraction","museum","point_of_interest","establishment"],"url":"https://maps.google.com/?cid=15074921902713971043","html_attributions":[]}', '', '', '']
预期的,我从当前迭代中得到的数据。
{"address_components":[{"long_name":"20","short_name":"20","types":["street_number"]},{"long_name":"West 34th Street","short_name":"W 34th St.","types":["route"]},{"long_name":"Manhattan","short_name":"Manhattan","types":["sublocality_level_1","sublocality","political"]},{"long_name":"New York","short_name":"New York","types":["locality","political"]},{"long_name":"New York County","short_name":"New York County","types":["administrative_area_level_2","political"]},{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"10001","short_name":"10001","types":["postal_code"]}],"business_status":"OPERATIONAL","formatted_address":"20 W 34th St., New York, NY 10001, USA","name":"Empire State Building","place_id":"ChIJaXQRs6lZwokRY6EFpJnhNNE","types":["tourist_attraction","museum","point_of_interest","establishment"],"url":"https://maps.google.com/?cid=15074921902713971043","html_attributions":[]}
有点不清楚,为什么需要帮助。一切正常。
如果你想跳过空元素,你可以在js端过滤:
let foo = $("#myForm").serialize().filter(bit => bit);
如果您只想发送列表中第一个非空元素:
let foo = $("#myForm").serialize().filter(bit => bit);
foo = foo.length ? foo[0] : {}
如果你想跳过空元素,你可以在python端过滤:
data = (bit for bit in serialized_data if bit)
如果您只想获得列表中第一个非空元素:
data = (bit for bit in serialized_data if bit)
data = next(data, None)
但我觉得,逻辑有问题。我无法定义它。
最后我的一条评论说,只需直接从字段中提取数据并将其存储在变量中,然后通过ajax调用将其发送回python