다음은 매핑 프로젝트에서 사용한 예입니다. Javascript는 플라스크 애플리케이션에서 Ajax를 통해 데이터를로드합니다.
JQuery ajax 메소드는 getJSON 메소드와 매우 유사합니다.
#ajax method to retreive well data for dynamic well values, x_pos, y_pos, substance concentration
@app.route('/getWellData', methods=['GET', 'POST'])
def getWellData():
#get all samples with that date
date_collected = request.args.get('date_collected')
site_id = request.args.get('site_id')
site_map_id = request.args.get('site_map_id')
substance_id = request.args.get('substance_id')
well_results = wellSubstanceDataBySite(
site_id=site_id,
site_map_id=site_map_id,
date_collected=date_collected,
substance_id=substance_id)
#return json to updateMarks ajax javascript function
return json.dumps(well_results)
자바 스크립트 :
//call the ajax endpoint for getWellData to return position, values, etc
$.ajax({
dataType: "json",
url: '/getWellData',
data: data,
success: function(data){
//iterate over each value in the data array and append it as div element to the .landmarks div
$.each(data, function(well_id, well){
//create the mark element, must be all mashed into one line, wont work with multiple lines
//sutract depth_of_water (well.value) from well.top_of_casing
var goundwater_elevation_val = well.top_of_casing - well.value
var mark = '<div class="item mark" id="well-' + well_id + '" data-id="' + well_id + '" data-position="' + well.xpos + "," + well.ypos + '" data-value="' + goundwater_elevation_val.toFixed(4) + '" data-show-at-zoom="0"><div><div class="text"><input class="well-checkboxes" type="checkbox" name="enable-well-' + well_id + '" checked style="margin:3px;"><strong>' + goundwater_elevation_val.toFixed(4) + '</strong></div><img src="/static/jquery-image-viewer/example/images/mark.png" width="50px" height="50px" alt="Permanent Mark" /></div></div>';
if (well.value != 0) {
//append the new mark to the .landmarks div
$('.landmarks').append(mark);
}
});
//refresh all landmarks to plot the new landmarks on the map with the smoothZoom API
$('#sitemap').smoothZoom('refreshAllLandmarks');
}
});
당신이 덤프하는 데이터의보다 정교한 예를 들어 줄시겠습니까 어떻게 당신이 자바 스크립트에서 참조 할 수 있습니까? –
당신이해야 할 일을 알고있는 것처럼 들리지만 JSON에서 값에 액세스하는 방법을 읽어야합니다. –