我正在尝试在加载页面时执行ajax调用以及我将通过此ajax调用获取的值我想将其指定为选择框值。但它不起作用。
GSP代码:
<select id="${form?.id}" name="${form?.id}" ><option selected="selected">select</option></select>
JAVASCRIPT代码:
$(document).ready(function(){
$.ajax({
url: "${g.createLink(controller: "landing", action: "getBankKeys")}",
data: {
country: $("#Country").text()
},
dataType: 'json',
success: function(json){
$.each(json, function(i, optionHtml){
$('#BankKey').append(optionHtml);
});
}
});
});
GROOVY CODE:
def getBankKeys() {
def bankKeys = vendorManagementDelegate.getBankKeys(params.country)
println "Bank Keys +++++++++++++++++++++++++++++++++++++++++++"+bankKeys
render bankKeys as JSON
}
我通过上面的println语句获得bankKeys作为这样的列表:
Bank Keys +++++++++++++++++++++++++++++++++++++++++++[0020000, 0020000, 0020001, 0020001, 0020007, 0020007, 0020067, 0020067, 0020149, 0020149, 0020199, 0020199, 0020215, 0020215, 0020218, 0020218, 0020243, 0020243, 0020245, 0020245, 0020255, 0020255, 0020265, 0020265, 0020274, 0020274, 0020278, 0020278, 0020315, 0020315, 0020345, 0020345, 0020348, 0020348, 0020371, 0020371, 0020389, 0020389, 0020505, 0020505, 0020506, 0020506, 0020531, 0020531, 0020557, 0020557, 0020558, 0020558, 0020567, 0020567, 0020588, 0020588, 0020662, 0020662, 0020680, 0020680, 0020681, 0020681, 0020693, 0020693, 0020779, 0020779, 0020870, 0020870, 0020885, 0020885, 0020891, 0020891, 0020922, 0020922, 0020943, 0020943, 0020978, 0020978, 0024006, 0024006, 0024138, 0024138, 0024150, 0024150, 0024152, 0024152, 0024165, 0024165, 0024227, 0024227, 0024291, 0024291, 0024329, 0024329, 0024344, 0024344, 0024350, 0024350, 0024430, 0024430, 0024468, 0024468, 0024618, 0024618, 0024658, 0024658, 0024692, 0024692, 0024730, 0024730, 0030000, 0030000, 0060000, 0060000, 0090000, 0090000, 0120000, 0120000, 0120001, 0120001, 0120002, 0120002, 0120004, 0120004, 0120005, 0120005, 0120006, 0120006, 0120007, 0120007, 0120008, 0120008, 0120009, 0120009, 0120010, 0120010, 0120011, 0120011, 0120012, 0120012, 0140000, 0140000, 0149200, 0149200, 0210000, 0210000, 0720000, 0720000, 123456, 123456, BANAMEX, BANAMEX, BANAMEX11, BANAMEX11, BANAMEX12, BANAMEX12, BANAMEX99, BANAMEX99, GEN01, GEN01, NONE, NONE, TRANSFERENCIA, TRANSFERENCIA]
答案 0 :(得分:1)
摆脱界限:
println "Bank Keys +++++++++++++++++++++++++++++++++++++++++++"+bankKeys
如果你要返回JSON,那必须是响应中的唯一内容。如果在它之前放置一个纯文本行,Javascript将无法解析JSON。
在Javascript中,您需要为数组元素创建<option>
元素:
$.each(json, function(i, option) {
$("#BankKey").append($("<option>", {
value: option,
text: option
}));
});