0
So... I loaded json array trought Ajax, then I used $.each to put id values in an [] array.
Then I wanted to check if specific number that is in the variable exist in the array, so I tried using inArray and also indexOf in many different ways, tried changing to strings and integers, and nothing helped. I did waste few hours already at this... 

Here's my Ajax code:

    $.ajax({
           method: "POST",
           url: "loadost.php",
            dataType: 'json',
           data: {
               "id" : id
           },
           success: function(ostloaded){
     ostlength = ostloaded.length;
               $.each($(ostloaded),function(key,value){

I added "loaded_id" to make sure it's integer, but it didn't help.

                var loaded_id = parseInt(value.id);
                idArray.push(loaded_id);
     typ.push(value.typ);
            link.push(value.link);
           tyt_art.push(value.tytul_autor);
         
           });
           

Here I sort where and when it should create elements

           for(q = 0; q<=ostlength;q++){
           if(typ[q] == 1){
         $('<li class="op'+q+'"></li>').appendTo('body #ops');
         $('<div/>').text(tyt_art[q]).appendTo('body .op'+q);
       }
       if(typ[q] == 2){
         $('<li class="ed'+q+'"></li>').appendTo('body #eds');
         $('<div/>').text(tyt_art[q]).appendTo('body .ed'+q);
       }
       if(typ[q] == 3){
         $('<li class="ins'+q+'"></li>').appendTo('body #ins');
         $('<div/>').text(tyt_art[q]).appendTo('body .ins'+q);
       }

Then I create playlist

       unflist.push(link[q]);
           }  
           list = unflist.filter(function (el) {
  return el != null;
});
           loadsongs(0, list);
           $(".playerHs").html(tyt_art[0]);
       }
       
    });



console.log($.inArray(pageId, idArray),idArray.indexOf(pageId), pageId, idArray);

the output no matter what - never changes, it's always like this: Console Log

I changed the names of the variables to make them clearer.

Nurarihyon
  • 27
  • 6
  • Define "after AJAX end". Are you trying to log the values before the asynchronous operation completes? Please edit your code to be a more complete and accurate representation of the problem. – David Aug 31 '20 at 19:00
  • If your `$.inArray` isn't in the `success` callback or wrapped in any sort of await logic, your code is saying *"Hey, make this call for me, and populate this array when you're done. **In the meantime**, check if this array has any items."* – Tyler Roper Aug 31 '20 at 19:02
  • Problem was with the Ajax, the code was executing, before array got filled... changing Ajax to synchronous fixed the problem. Thank you for the fast response. – Nurarihyon Aug 31 '20 at 19:27
  • 1
    `async: false` is never, *ever*, a good idea. Not to mention that it's deprecated in most browsers. Chrome (and I think Firefox) will even throw a console warning to anyone visiting your page. – Tyler Roper Aug 31 '20 at 19:30
  • Yeah, just noticed that it disabled the other function... What should I do then? – Nurarihyon Aug 31 '20 at 19:34
  • @Nurarihyon: Log your values to the console in the `success` callback instead of outside of the AJAX entirely. – David Aug 31 '20 at 19:53
  • @David u mean the whole content? – Nurarihyon Aug 31 '20 at 20:05
  • @Nurarihyon: I mean that whatever operation you want to perform after the AJAX data is returned should be performed after the AJAX data is returned. Currently in the code shown that operation is just a `console.log`, so simply move that operation into the end of the `success` callback function. – David Aug 31 '20 at 20:09
  • @David Oh, it's only to show the output, the function I have, doesn't work when I put it in Ajax – Nurarihyon Aug 31 '20 at 20:13
  • @Nurarihyon: Well we certainly can't help with a problem that hasn't been described in code that hasn't been shown. But the general rule remains... Use the results of the AJAX operation after you receive them, not before you receive them. – David Aug 31 '20 at 20:16
  • @David I'll try tomorrow, if im not gonna find the solution, I'll ask another question. Thank you for the tip. – Nurarihyon Aug 31 '20 at 20:21
  • @David So... I almost figured it out, but new strange problem occured. Im trying to call a function in the another function to load the song, and in one case it works fine, but in the other one for some reason not. Console.Log returns values the same way in both cases, Im not sure why it is not working... any ideas? – Nurarihyon Sep 01 '20 at 15:56
  • Nevermind, just had to parseInt the variable in one of the cases for some reason. – Nurarihyon Sep 01 '20 at 16:05
  • @Nurarihyon: The same response still applies... I can't really tell you what's wrong with code I can't see or a problem that hasn't been described. – David Sep 01 '20 at 16:05

0 Answers0