如何将JSON渲染为jQuery

时间:2019-04-23 04:41:14

标签: jquery json grails

如何将.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))
    })
})

1 个答案:

答案 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)
    },
});