0

I want to render an object from my controller to a jQuery callback function. That is how I try to do it:

Calling jQuery Ajax function

function addVideo() {
    var url = "/myApp/project/addVideo"
    var data = 'videoURL=' + $('#videoUrlInputText').val()

    $.getJSON( url,  
            {
                videoURL: $('#videoUrlInputText').val()
            }, 
            updateVideoLoad(data)
    )
}

My Grails 'Project' controller

def addVideo() {
    def videoMap = [urlAccepted: "bim", provider: "bam"]
    render videoMap as JSON
}

My jQuery callback function (updateVideoLoad())

function updateVideoLoad(videoMap) {
    $('h4').html( 'Provider: ' + videoMap.provider )
}

I end up getting the undefined output in the browser. Nothing in the browser console. The controller output is tested and renders correctly the JSON object.

Any idea why the object is not read correctly by the callback function ? Any suggestion is most welcome.

Thanks in advance.

1 Answer 1

2

I don't know the particulars, but I suspect your success callback is being problematic, since it is not just a function name or anonymous function.

What if you try this?

$.getJSON( url,  
        {
            videoURL: $('#videoUrlInputText').val()
        }, 
        function (response, status, jqxhr) {
             updateVideoLoad(data)
        }

)
Sign up to request clarification or add additional context in comments.

2 Comments

Bingo. Encapsulating my function call into "function(data) {}" solved it. I am not sure why the function was not resolved correctly. What do you mean by "it is not just a function name" ? Thanks a lot !
If you had a function defined somewhere like this: function updateVideoLoad(data, status, jqxhr) {...} you would reference it from within the ajax call like this: success: updateVideoLoad or $.getJSON(url, data, updateVideoLoad);` Having fn(data) makes it fire immediately instead of as a callback (guessing a little).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.