Arbusto Arbusto - 1 year ago 68
Ajax Question

Use jQuery ajax on multiple URLs

I'm trying to connect to a same URL multiple times at once but with different page number at the end (parameter) to get on each one a specific class and save it as a list to access later but my code is not working, although I tried different approaches.

Basically I'm counting the total number of pages on the main page (, then trying to connect through $.get to each one and getting a specific class to have everyone ready for later using it (you can try adding it in Chrome Dev Console to try it):

// Get the total amount of pages
var totalPagesCount = $('.paginator__item:last-child a').attr('href').split('/')[2];

// Get every employer from the all the pages
for(var i=0; i<totalPagesCount; i++) {
$.get('' + i, function(data) {
var page = $(data);
var pageContent = page.filter('li.lister__item');


Thank you for your help, please let me know any other information you may need!

Answer Source

A couple of things...there is not a "0" zero page on the site. So you need to do this:

for(var i=0; i<(totalPagesCount+1); i++) {
  $.get('' + (i+1), function(data) {

Secondly, that page is sending a 301 permanent redirect:

enter image description here

So you'll need to handle the redirect in your ajax call. That, in my mind, is a new question - but have no fear - there are other answers for that (in spades). For instance When a response to $.ajax is 301, can I programmatically get the new URL?.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download