0

I can't understand why my function returns empty date. If I use REST client it returns my correct date.

function requestData(data) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url + data, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var info = xhr.responseText;
            return info;
        }
    };
    xhr.send();
}

Is anyone can explain me why I retrieve empty data?

Alex
  • 2,091
  • 6
  • 31
  • 49
  • You can't return from AJAX request. Read detailed description of the problem in the linked question. – dfsq Nov 01 '14 at 23:51
  • I think this question isn't duplicate. The question marked as duplicate mention jQuery and ajax, here it is raw XMLHTTP manipulations. –  Nov 01 '14 at 23:51
  • I don't use jQuery. So the questions aren't duplicates. – Alex Nov 01 '14 at 23:51
  • You either have to set up an asynchronous callback or perform the request synchronously – Jonathan Gray Nov 01 '14 at 23:53
  • 1
    XHR is working properly. The issue is library agnostic, so while the dupe uses jQuery, the issue you're asking about is the same. –  Nov 01 '14 at 23:55
  • 1
    @RobinCarlier: The issue is the same. Doesn't matter what (if any) library is used. All that does is put the above code behind a generic function call. –  Nov 01 '14 at 23:57

0 Answers0