1

How can I access the Header of and ajax response from the rest API?

I need the total number of pages (X-WP-TotalPages)

Here my code:

$(document).ready(function () {
  var pageCounter = 1;
  var ourHTMLString = '';



/////////////////////////////////////
// Ajax function starts            //
/////////////////////////////////////

  function llamadaAjax() {
    $.ajax({ //comienza ajax
    type: 'GET',
    url: magicalData.siteURL + '/wp-json/wp/v2/posts?_embed&categories=5&page=' + pageCounter,
    success: function (articulos) {
    $.each(articulos, function (index, articulo) {
        ourHTMLString += '<article class="post">';
        ourHTMLString += '<div class="post-thumbnail fade1"><img src="' + articulo._embedded['wp:featuredmedia'][0].media_details.sizes['small-thumbnail'].source_url + '"></div>';
        ourHTMLString += '<a href="' + articulo.link + '"><h3>' + articulo.title.rendered + '</h3></a>';
        ourHTMLString += '<p>' + articulo.content.rendered + '</p>';
        ourHTMLString += '</article>';

      }); 

      $('#criticasPostContainer').html(ourHTMLString);
    }
  }); 

  // fin ajax

}

///////////////////////////////
  // fin FUNCIÓN llamada ajax
///////////////////////////////


 llamadaAjax();

 // // click event listeners

$('button.botonSigCriticas').click(function(){
  pageCounter++;
  ourHTMLString = '';
  llamadaAjax();
  });

$('button.botonAntCriticas').click(function(){
  pageCounter--;
  ourHTMLString = '';
  llamadaAjax();
  });


  });// end jquery function
1

You can use change ajax this: request.getResponseHeader('your_header')

$.ajax({
    type: 'POST',
    url:'api-url.php',
    data: formData,
    success: function(data, textStatus, request){
        alert(request.getResponseHeader('your_header'));
    },
    error: function (request, textStatus, errorThrown) {
        alert(request.getResponseHeader('your_header'));
    }
});
1
  • Sorry but I don't understand. Can you b more specific? I dont have an error property, nor a post type, it's a GET. If i add a third argument to my success property it returns a lot of info where i cannot find the X-WP-TotalPages – Marcos Di Paolo Jul 29 '17 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.