ttt
ttt

Reputation: 4004

Grails Ajax URL createLink doesn't work

My code snippet is:

$('#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);
        }
    });

However it doesn't work. I use chrome web tools to track the ajax call url is something like

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

Why grails can't interpret the createlink to the actual url?

Upvotes: 2

Views: 2135

Answers (1)

andy mccullough
andy mccullough

Reputation: 9541

What I usually do in this case is in a GSP file, do something like :

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

Then in the AJAX call in the JS file do :

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

There may be other, better ways, but it works for me

Upvotes: 3

Related Questions