Home>

I want to get X-WP-TotalPages in http header information after ajax communication to WordPress RESTAPI URL.
I tried to get header information with the following code, but it ended up getting an error.
Thanks for your teaching.

function newsGetter (dom, rest_url) {
    var domArea = dom;
    $.ajax ({
        type: 'GET',
        url: rest_url,
        crossDomain: true,
        contentType: "application/json;charset = UTF-8",
        cache: false,
        dataType: 'json'
    }). done (function (wordpressapi, xhr) {
        var lng = wordpressapi.length;
        console.log (xhr.getResponseHeader ('X-WP-TotalPages'));
        if (lng>5) {
            lng = 5;
        };
        for (var i = 0;i<lng;i ++) {
            var title = wordpressapi [i] ['title'] ['rendered'];
            var id = wordpressapi [i] ['id'];
            var content = wordpressapi [i] ['content'] ['rendered'];
            var link = wordpressapi [i] ['link'];
            var excerpt = wordpressapi [i] ['excerpt'] ['rendered'];
            var category = wordpressapi [i] ['_ embedded'] ['wp: term'] [0] [0] ['name'];
            var catslug = 'cat' + wordpressapi [i] ['_ embedded'] ['wp: term'] [0] [0] ['slug'];
            if (wordpressapi [i] ['_ embedded'] ['wp: featuredmedia']) {
                var image = wordpressapi [i] ['_ embedded'] ['wp: featuredmedia'] [0] ['media_details'] ['sizes'] ['post-thumbnail'] ['source_url'];
                var image_html = '' + '' + '';
                var image_css = 'image-on';
            } else {
                var image_html = '';
                var image_css = 'image-off';
            }
            var dateData = new Date (wordpressapi [i] ['date']);
            yy = dateData.getYear ();
            if (yy<2000) {
                yy + = 1900;
            }
            mm = dateData.getMonth () + 1;
            if (mm<10) {
                mm = "0" + mm;
            };
            dd = dateData.getDate ();
            if (dd<10) {
                dd = "0" + dd;
            };
            var postDate = yy + '-' + mm + '-' + dd;
            var html =
                '' +
                '' +
                '' +
                '' +
                '' + postDate + '' +
                '' + title + '' +
                '' +
                // '<span>' + category + '</span>' +
                // '' + content + '' +
                '' +
                image_html +
                '' +
                '';
            $(domArea) .append (html);
        }
    }). fail (function (wordpressapi) {
        $(domArea) .append ("Data reading failed");
    });
};
<script src = "index.js"></script>
<script>
    $(function () {
        newsGetter ("#newslist", "https: //example.copm/wp-json/wp/v2/posts? _embed&per_page = 5");
    });
</script>
  • Answer # 1

    See the third argument for done

    $(function () {
      $.ajax ({
      url: rest_url,
      }). done (function (data, status, xhr) {
        console.log (data);
        console.log (xhr.getResponseHeader ("x-wp-totalpages"));
      });
    });

Related articles