0

I have an HTML page where a button click will fire an ajax function showDetail():

<a href="#" class="list-group-item" onclick="showDetail({{ test.id }});">
<strong>{{ test.test_name }}</strong></a>

The ajax function works well if it's like this on its own:

function showDetail(id){
    $.ajax({
    url : "/test/" + id,
    type: 'GET',
    success: function(data){
        map.removeLayer(tests);
        $('#pane-content-edit').html(data);
        $('.tests_display').animate({right:0});
    }
});
}

I would like to modify it having the current HTML page containing a Geojson. And I'd like to pass it to my ajax to pass it to the url my ajax is pointing at. So I did this:

function showDetail(id){
    $.ajax({
    url : "/test/" + id,
    type: 'GET',
    data: {'tests_geojson' : {{ tests_geojson | safe}}},
    success: function(data){
        map.removeLayer(tests);
        $('#pane-content-edit').html(data);
        $('.tests_display').animate({right:0});
    }
});
}

After adding that, it would not fire. I tried opening up the web console, nothing is happening. Can someone help? or tell me how to debug this?

Thank you!

Edit: Additional info - Django Framework templating

Contents of test_geojson showing on Leaflet map so it's most likely validated: Big geojson feature but would look generally like so:

alert(JSON.stringify(geojsonFeature));



{ "type": "FeatureCollection",
      "features": [
        { "type": "Feature",
          "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
          "properties": {"prop0": "value0"}
          },
        { "type": "Feature",
          "geometry": {
            "type": "LineString",
            "coordinates": [
              [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
              ]
            },
          "properties": {
            "prop0": "value0",
            "prop1": 0.0
            }
         }
         ]
       }

Edit: Temporary Solution

This looks like on the right direction. Serializing the JSON data to string, adding csrf, then modify things at views.py. However an error occurs, saying that it has exceeded Data Upload Max Memory Size so I had to set DATA_UPLOAD_MAX_MEMORY_SIZE = None which is a security no-no. If someone has a better workaround it'll be helpful!

var geojsonFeature = {{tests_geojson|safe}};
function showDetail(id){
    $.ajax({
    beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        },
    url : "/test/" + id,
    type: 'POST',
    data: {'tests_geojson' : JSON.stringify(geojsonFeature)},
    success: function(data){
        map.removeLayer(tests);
        $('#pane-content-edit').html(data);
        $('.tests_display').animate({right:0});
    }
});
}
Reiion
  • 923
  • 3
  • 15
  • 33

0 Answers0