0

I need to send request on events keyUp/keyDown etc. I Implemented ajax calls and the first call works good but its extremely slow and more often freezes the input field. I would like to go with the second call but the problem is, i cannot access an array object that is populated after success. Although the array object has response on it, the length is always 0. [Image of console.log][1] I would appreciate if i could get help with it. Thank you in advance.

   //First Call
        apiCallBind: function(){
              var ajaxReturn = $.ajax({
                        url: "example.php",
                        data: {currentQuery: "test"},
                        async: false,
                        global: false
                    }).responseText; }
    return ajaxReturn.items;
    }

   //Second Call
    apiCallBind: function () {
            var url ="example.php";
            var test = [];
            $.ajax({
                url: url,
                data: {currentQuery: this.query()},
                success: function (response) {
                    test.push(response);
                }
            });

            console.log("Test Length::"  + test.length);
            console.log(typeof test);
            console.log(test);
        }


  [1]: https://i.stack.imgur.com/E607A.png
Test
  • 1
  • 4
  • 1
    By no means should you ever run a synchronous http request. EVER. Instead, you can run the second request in success by using a callback function. – Brandon Mowat Nov 23 '17 at 19:55
  • Please make yourself used to async code ( by reading the really good duplicate ) then you can probably solve the problem yourself or ask a more detailed question – Jonas Wilms Nov 23 '17 at 19:59

0 Answers0