How to call ajax? iteratively by Jquery

now there is such a requirement, for example, I have an array:

    arr=[
           {    
                id:"84001",
                name:""
            },{    
                id:"84002",
                name:""
            },{    
                id:"84003",
                name:""
            },{    
                id:"84004",
                name:""
            }, 
    ]

I want to get the url, of all the music by calling API through the id in the array. If I call ajax through the traversal array, I get the Url? of the music.

var urlList=[]
arr.forEach((item)=>{
    $.ajax({
    url:"api/v1/getMusicById?id="+item.id,
    type:"get",
    success:(res)=>{
        urlList.push(res.url)
    }
    
    })

})
console.log(urlList)

this is what I thought at first, but there must be something wrong with this. The ajax request is asynchronous
I want to complete the ajax request after the traversal is completed, and then I can get a url list
how can you implement it?


< del > Promise.all , or recursively call and delete the array item until it is 0, which is even easier if you can use async/await . < / del >
none of the above is correct. The correct thing to do is to ask your backend to return url . What does it mean to have more than ten requests for one piece of data?


just use Synchronize

async: false,

it is recommended to change it to post request
request parameters. All id are grouped into an array in the request parameters, and all url are returned uniformly at the backend

.
var urlList=[]
var idList = [1,2,3,4,5]
$.ajax({
    url:'api/v1/getMusicByIds,
    type:'post',
    data:idList,
    success:(res)=>{
        urlList = [...res.url]
    } 
})

if the asynchronous request is nested inside the loop, the asynchronous result will not be returned at the end of the loop. Try a layer of closure on the outside. If forEach does not work, replace it with for,. I have used asynchronous request in for loop. I do not know whether forEach will work without forEach,.

var urlList=[]
arr.forEach((item)=>{
    (function(){
        $.ajax({
        url:'api/v1/getMusicById?id='+item.id,
        type:'get',
        success:(res)=>{
            urlList.push(res.url)
        }
        
        })    
    })()
})
console.log(urlList)

I think there is something wrong with your needs

generally, url that requires music is needed when playing, and only one song is played at a time, so you can execute an ajax request to get url through id

when playing.

set a variable index=0; after each success and then determine whether arr [index] has a value. In some cases, it is a good choice to continue ajax, recursion


is a problem of requirements. Even if the function is done, it is not possible to send so many requests at once. The server explodes and changes it to the request list

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bde105-313b7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bde105-313b7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?