Arbusto Arbusto - 10 days ago 5
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 (https://careers.insidehighered.com/employers/), 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('https://careers.insidehighered.com/employers/' + i, function(data) {
var page = $(data);
var pageContent = page.filter('li.lister__item');

console.log(pageContent);
});
}


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

Answer

A couple of things...there is not a "0" zero page on the https://careers.insidehighered.com/employers/ site. So you need to do this:

for(var i=0; i<(totalPagesCount+1); i++) {
  $.get('https://careers.insidehighered.com/employers/' + (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?.

Comments