herom
herom

Reputation: 2542

Grails jQuery Ajax request - URL not working

I searched the net and also here at stackoverflow, but I couldn't find any hint on this...

I've got a Grails 2.1.1 project and I want to call a Controller function from jQuery with an Ajax request. So far, I come up with this:

$.ajax({
    type: 'POST',
    url: "${createLink(controller:'userSearch',action:'ajaxFindUser')}",
    dataType: 'json',
    data: {
        lastname: $('#searchLastName').val(),
        firstname: $('#searchFirstName').val(),
        zipcode: $('#searchZipCode').val(),
        city: $('#searchCity').val()
    },
    success: function(data) {
        $('#searchSubContainerBody').html(data);

        $('#searchTable tr').draggable({
           cursor: 'move',
           helper: 'clone',
           scope: 'drag-guests',
            start: function(event, ui) {
              searchCloneTableRow.tr = this;
                searchCloneTableRow.helper = ui.helper;
                searchCloneTableRow.cells = new Array();
                $.each(this.cells, function(index, column) {
                    searchCloneTableRow.cells.push(column.innerText);
                });
            },
            connectWith: '#searchDetailContainerDropArea'
        });

        $('searchTable').dataTable({
            'bJQueryUI':true
        });

    },
    error: function(request, status, error) {
        $(errorDialog).html(error);
        $(errorDialog).dialog('open');
        return false;
    },
    complete: function() {
        //do something
    }
});

and when I click on the button to fire the Ajax request, the DevTools show the following error:

POST > http://localhost:8080/GrailsTest001/authentication/$%7Bg.createLink(controller:'userSearch',action:'ajaxFindUser')%7D 404 (Not Found) 

so, as you can see, the URL get's HTML encoded... how can I prevent Grails from encoding it?

in Config.groovy the codec is set to none:

// The default codec used to encode data with ${}
grails.views.default.codec = "none" // none, html, base64

what am I doing wrong here? could you give me any hint (or better: a solution) for this? any help is greatly appreciated!

Upvotes: 2

Views: 4765

Answers (1)

user553180
user553180

Reputation: 626

The GSP snippet

 ${createLink(controller:'userSearch',action:'ajaxFindUser')}

is not getting interpreted - is this in a javascript file or in actual GSP? It looks like it's defined in a javascript file. If so, it won't work, you'll need to create a javascript var on the GSP page, and then reference that javascript var from your ajax call. For example:

In your GSP code:

<g:javascript>
  var ajaxAction = "${createLink(controller:'userSearch',action:'ajaxFindUser')}"
</g:javascript>

In your javascript file containing the ajax code:

$.ajax({
    type: 'POST',
    url: ajaxAction,
    dataType: 'json',
    data: {
  ...

Upvotes: 4

Related Questions