【问题标题】:How to render json to jquery如何将json渲染到jquery
【发布时间】:2019-04-23 04:41:14
【问题描述】:

如何将资产目录中的.json 文件渲染到grails 中的jquery 脚本?这是我要在其中呈现 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))
    })
})

【问题讨论】:

  • 你打错了:entry,name 应该是entry.name
  • 可以简化为$("&lt;option&gt;", {value: entry.abbreviation, text: entry.name})

标签: jquery json grails


【解决方案1】:

在控制器中

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



【讨论】:

    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多