gpt4 book ai didi

jquery - Grails Ajax URL createLink 不起作用

转载 作者:行者123 更新时间:2023-12-01 03:40:48 25 4
gpt4 key购买 nike

我的代码片段是:

$('#PostCode').autocomplete({
source: function (request, response) {
$.ajax({
url: "${createLink(controller:'postcode',action:'getValidPostcodeValues')}",
dataType: "json",
success: function( data ) {
response( $.map( data, function( item ) {
return {
id: item.id,
value: item.name
}
}));
}
});
},
minLength: 1,
select: function (event, ui) {
$('#PostCodeHidden').val(ui.item.id);
}
});

但是它不起作用。我使用 chrome web 工具来跟踪 ajax 调用 url,类似于

GET http://localhost:8080/edp-grails/xxx/xxx/$%7BcreateLink(controller:'postcode',action:'getValidPostcodeValues')%7D 404 (Not Found)

为什么 grails 无法将 createlink 解释为实际的 url?

最佳答案

在这种情况下我通常做的是在 GSP 文件中,执行如下操作:

<script> var getValidPostcodeValuesURL = "${createLink(controller:'postcode',action:'getValidPostcodeValues')}"</script>

然后在 JS 文件中的 AJAX 调用中执行以下操作:

$.ajax({
url: getValidPostcodeValuesURL,
dataType: "json",
success: function( data ) {
response( $.map( data, function( item ) {
return {
id: item.id,
value: item.name
}
}));
}
});

可能还有其他更好的方法,但它对我有用

关于jquery - Grails Ajax URL createLink 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com