Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dan3460's avatar

Returning data from Ajax request

Originally i had this function:

        $.ajax({
            url: "/albums/getAlbumData",
            data: {
                albumID: albumID,
                dataType: dataType
            }
        })
        .done(function(data){
            $("#infoModalData").text(data));
        });

within a block that was triggered when pressing a button. We are adding more buttons so it makes sense to push the ajax call to a function:

 function getAlbumData(albumID, dataType) {
        $.ajax({
            url: "/albums/getAlbumData",
            data: {
                albumID: albumID,
                dataType: dataType
            }
        })
        .done(function(data){
            console.log(data);
            return(data);
        });

    };

The console.log() shows the correct data but it doesn't return anything to update the DOM. What i'm doing incorrectly.

0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

First of you are not actually returning anything in the function. You are returning something inside a sub function. Secondly your call will be async and not return anything at once.

An easy fix is this https://stackoverflow.com/a/5316755/1305117

dan3460's avatar

@sinnbeck thanks for the answer. As you can see i'm not very proficient in jQuery or JavaScript. I do understand that is an async call, but i thought that the .done() waited for the answer to continue. In any case the sample you gave works.

Thanks again.

Please or to participate in this conversation.