0

I have the following code:

my_string.replace(/^\[poll:([^\]]+)\]/mgi, function(match, capture) {
    return $.ajax({
        type: 'GET',
        url: '/my_url,
        data: {
            text: match
        },
        success: function(response) {
            return response;
        }
    });
});

I am doing an AJAX call in the anonymous function which returns the content that I would like to replace the string with but it is not working. It appears that I am returning the AJAX function rather than the response content.

Allen Liu
  • 3,948
  • 8
  • 35
  • 47
  • 3
    you can't do that because ajax is asynchronous!!! the `$.ajax()` returns a promise object, not the response of the ajax request – Arun P Johny Jan 31 '14 at 02:42

1 Answers1

1

The first A in AJAX stands for Asynchronous. $.ajax does not return data synchronously like that.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368