2
        function ChatServerQuery(data_json) {

        var result = null;

        $.ajax({
            url: 'chat/backend/',
            type: 'POST',
            data: data_json,
            success: function(json) {
                result = json
            }
        })

        return result

    }

My function that executes a request to the server. The problem is that I can not return received from the server text. I do not know how to take from an anonymous function (event success) to ChatServerQuery (where you can easily get it back).

Ticksy
  • 561
  • 2
  • 10
  • 19
  • I answered something similar earlier: http://stackoverflow.com/questions/9054897/why-cant-change-the-value-from-inside-jquery-post-function-javascript/9054910 – James M Jan 29 '12 at 19:01
  • @JamesMcLaughlin That is my function completes before start the event? If I disable asynchronous, then it will work? – Ticksy Jan 29 '12 at 19:04
  • Yes, but that's the wrong approach (disabling `async` _will_ cause problems). You should understand that the data isn't available until your callback is executed, and write your code accordingly. – James M Jan 29 '12 at 19:06
  • @JamesMcLaughlin The only problem is that I need to change the logic of the code? – Ticksy Jan 29 '12 at 19:12

5 Answers5

12

You'd better change your approach to reflect an asynchronous nature of AJAX request.

Using callback function

function ChatServerQuery(data, callback) {
    $.ajax({
        url:  'chat/backend/',
        type: 'POST',
        data: data,
        success: callback
    });
}

Then you would use it:

ChatServerQuery(dataObject, function(data) {
    // work with your data came from server
});

Using promise object

$.fn.ajax returns object implementing Promise iterface, so you can use it like this:

function ChatServerQuery(data) {
    return $.ajax({
        url:  'chat/backend/',
        type: 'POST',
        data: data
    });
}

ChatServerQuery(dataObject).done(function(data) {
    // work with your data came from server
});

This option offers you more flexibility.

dfsq
  • 191,768
  • 25
  • 236
  • 258
1

I answered something similar earlier:

Your function returns result long before the AJAX callback executes. Anything that depends on the result of the request has to happen in or after the callback.

Community
  • 1
  • 1
James M
  • 18,506
  • 3
  • 48
  • 56
0

function ChatServerQuery(url,data) {

return $.ajax({ type: "POST",
    url: url,
    data: data,
    async:false,
    success: function(status){
    }
}).responseText;

}

//Now you will get the response text from server side via ajax call

Krishna
  • 9
  • 3
0

Instead of trying to assign json to result in the success handler, do your logic there (or in functions called from it). Or if you need to wait for the response, follow the other comments.

clime
  • 8,695
  • 10
  • 61
  • 82
-2

The request needs to be synchronous to work in this way.

Add async: false to the parameters object of the ajax() method.

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69