I
I
IgorPlays2021-10-25 12:08:21
AJAX
IgorPlays, 2021-10-25 12:08:21

How to get Ajax status into a variable?

I have a code that iterates over the API, and I wanted to make it stop when there was an empty request in the chicle.
Here is the code

for (let i = 0; i < 250; i++) {
$.ajax({
            url: "api/page="+i,
            type: 'GET',
            dataType: 'json', 
            async: false,
            success: function(res) {
              for (let i = 0; i < 250; i++) {
                var id = res['_embedded']['tasks'][i]['id'];
                if(id_task =="undefined" || id_task == null || id_task == ''){
                  break;
                }

But it does not work, he continues to work further, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-10-25
@zkrvndm

for (var i = 0; i < 250; i++) {
    
    var response = await $.ajax('api/page=' + i);
    
    console.log('Получены данные:');
    console.dir(response);
    
    // Здесь впиши условие остановки:
    
    if (условие) {
        
        i = 250; // Увеличиваем счетчик до 250, это приведет к остановке цикла
        
    }
    
}

// Здесь пиши дальше свой код

I have no idea what the server returns to you, so enter the stop condition yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question