How to set ajax return value as a variable

I have the following ajax call that succeeds:

function fnFormatDetails ( oTable, nTr )
   {
            var aData = oTable.fnGetData( nTr );

            var memberid = 'memberid='+ aData[6];

            $.ajax({
                type: "POST",
                url: "shout.php",
                data: memberid,
                success: function(html) {
                    //$("#shout").html(html);   
                    var sOut = html.returned_val;
                }
            });

            return sOut;
        }

If I delete the comment line ($ ("shout"). Html (html) and use the div on my page, the results are displayed in order. However, there is a second function that will use the HTML results from sOut and accordingly display them in the correct position.

The php file in shout.php simply "echos" the HTML to the page (which is then returned and displayed accordingly.

Unfortunately, I cannot set the sOut variable currently based on the results of my ajax call. What am I missing?

+3
source share
3 answers

, , AJAX, . , , "shout.php" . JSON , dataType $.ajax.

function fnFormatDetails ( oTable, nTr ) {
    var aData = oTable.fnGetData( nTr );
    var memberid = 'memberid='+ aData[6];

    var result;

    $.ajax({
        type: "POST",
        url: "shout.php",
        data: memberid,
        async: false,
        success: function(data) {
            result = data;
        }
    });

    return result;
}
+5

sOut , fnFormatDetails.

-, $.ajax , , "return sOut", !

- , html.returned_val, async: false $.ajax, .

+2

@Matt Huggins

async: false

, , .:)

+1

Source: https://habr.com/ru/post/1795896/


All Articles