簡體   English   中英

在使用 flask、ajax 繪制 Leaflet 之后重新渲染頁面

[英]Rerender page after Leaflet drawing with flask, ajax

嗨,我正在使用 Flask 和 Leaflet 地圖開發應用程序。 map 上有幾何對象。 在繪圖工具的幫助下,用戶在 map 上繪制多邊形形狀,並且所有落在形狀內的 object 數據(即“id”)應呈現在 html 表中。 繪制形狀后,POST 請求就完成了。 雖然我可以在 traceback 中打印出數據並在 Devtools Network Preview 選項卡中看到它,但我仍然無法在 html 中呈現這些數據。

這是我在繪制形狀后發送 Post 請求的 JS 代碼。 output 給出形狀頂點坐標。

mymap.on('pm:create', e => {
    var map_data = e["layer"]["_latlngs"][0];
    console.log(map_data);
    $.ajax({
      type: "POST",
      contentType: "application/json",
      url: "/",
      traditional: "true",
      data: JSON.stringify({map_data}),


      });
});

然后我用這個坐標分配一個變量並在 app.py 中打印出來。 使用不同的地理工具功能,我還資助了多邊形內的東西:

shape_coord = request.get_json() //gets shape coordinates

if shape_coord:
 filter_markers = marker_within_polygon(markers, shape_coord) //function finds markers within shape
 filter_lines = lines_within_polygon(lines, shape_coord)//function finds lines within shape

 print(shape_coord)// OK
 print(filter_markers, filter_lines)//OK. prints out stuff inside polygon

這是我的 jinja2:

<tbody id="table_body">

                        {% for mk in  filter_markers%}
                            <tr>
                                {% for ln in filter_lines %}
                                     <th>{{ ln }}</th>
                                     <th>{{ mk }}</th>
                                {% endfor %}
                             </tr>
                        {% endfor %}
 </tbody>

我嘗試像這樣在 Ajax 中使用成功回調,但除了所需的數據之外,它還會在表中插入整個 HTML 正文標記:

mymap.on('pm:create', e => {
    var map_data = e["layer"]["_latlngs"][0];
    console.log(map_data);
    $.ajax({
      type: "POST",
      contentType: "application/json",
      url: "/",
      traditional: "true",
      data: JSON.stringify({map_data}),
      success: function (data) {
                    $("#table_body").html(data);

      });
});

我怎樣才能只渲染我的標記和線條數據?

我解決了這個問題。 成功取決於正確的問題)我在這里找到了答案: Query ajax response not displayed when returned to current page

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM