jQuery AJAX solution inside each() loop

后端 未结 3 1840
暖寄归人
暖寄归人 2020-12-07 06:41

I have noticed that I\'m having problems when I\'m using AJAX in jQuery inside a .each() loop. Only the first record in my database are being updated when the script execute

3条回答
  •  猫巷女王i
    2020-12-07 06:57

    There are a couple things happening here.

    1. By re-using the request variable in the loop (scoped outside the loop), every iteration of the loop assigns a new object to request, overwriting what was there. This means you are only setting the response handlers for the last iteration.

    2. Your request.done method reloads the page, which in practice will halt the other requests and...

    3. You are looking up the same userTypeId for each iteration of the loop, as mentioned by @Sean

提交回复
热议问题