如何将.json
文件从资产目录渲染到jquery
中的grails
脚本中?这是我要呈现json文件的jquery代码:
let dropdown = $('#banks');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Select bank</option>');
dropdown.prop('selectedIndex', 0);
const url = '';
$.getJSON(url, function (data) {
$.each(data, function (key, entry){
dropdown.append($('<option></option>').attr('value', entry.abbreviation).text(entry,name))
})
})
答案 0 :(得分:0)
在控制器中
import grails.converters.JSON
import asset//ur asset
class renderJsonController {
def renderJson() {
def result = asset.getJsonFromAsset()//Getter function in asset to get target json
render result as JSON
}
}
.gsp
jQuery.ajax({
url: '${createLink(controller: 'renderJson', action: 'renderJson')}',
data: {},
type: 'POST',
dataType: 'json',
success: function (json) {
//!!get json here!!
},
error: function (xhr, ajaxOptions, thrownError) {
defaultAjaxError(xhr)
},
});