【问题标题】:Grails Ajax URL createLink doesn't workGrails Ajax URL createLink 不起作用
【发布时间】:2014-01-07 07:33:00
【问题描述】:

我的代码 sn-p 是:

$('#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.js 文件?
  • 同意伊戈尔。您所说的表明${} 不是由常规代码解释的,而是被视为字符串。 ${createLink...} 必须在 GSP 文件中
  • 对。应该在 gsp 文件中。谢谢

标签: jquery ajax url grails


【解决方案1】:

在这种情况下,我通常会在 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
                    }
                }));
            }
        });

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多