The Canvas API uses pagination for requests that return multiple items. The URL of the next result set is specified in an HTTP response header. I know how to get the response body, but how do I get the response headers from a jQuery AJAX call?
The jQuery ajax documentation indicates that the “[jqXHR] object returned by $.ajax() as of jQuery 1.5 is a superset of the browser’s native XMLHttpRequest object.” And the XMLHttpRequest object includes a getResponseHeader method.
At first I tried calling getResponseHeader as a method of the data object:
$.ajax({
url:'/api/v1/courses'
}).done(function (data) {
console.log(data.getResponseHeader('Link'));
});
But data
contained just the response body, and data.getResponseHeader
was null.
A closer look at the jQuery documentation indicated that additional objects are passed to the done function, including the jqXHR object. Accessing that object’s getResponseHeader method worked:
$.ajax({
url:'/api/v1/courses'
}).done(function (data, textStatus, xhr) {
console.log(xhr.getResponseHeader('Link'));
});