{"id":1103,"date":"2014-08-30T23:14:17","date_gmt":"2014-08-31T04:14:17","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=1103"},"modified":"2014-08-30T23:16:30","modified_gmt":"2014-08-31T04:16:30","slug":"using-getresponseheader-with-jquerys-ajax-method","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2014\/08\/using-getresponseheader-with-jquerys-ajax-method\/","title":{"rendered":"Using getResponseHeader with jQuery&#8217;s ajax method"},"content":{"rendered":"<p>The <a href=\"https:\/\/canvas.instructure.com\/doc\/api\/\">Canvas API<\/a> uses <a href=\"https:\/\/canvas.instructure.com\/doc\/api\/file.pagination.html\">pagination for requests that return multiple items<\/a>. 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?<\/p>\n<p>The <a href=\"http:\/\/api.jquery.com\/jquery.ajax\/\">jQuery ajax documentation<\/a> indicates that the &#8220;[jqXHR] object returned by $.ajax() as of jQuery 1.5 is a superset of the browser&#8217;s native XMLHttpRequest object.&#8221; And the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/XMLHttpRequest\">XMLHttpRequest object<\/a> includes a <em>getResponseHeader<\/em> method.<\/p>\n<p>At first I tried calling <em>getResponseHeader<\/em> as a method of the <em>data<\/em> object:<\/p>\n<pre><code>$.ajax({\r\n    url:'\/api\/v1\/courses'\r\n}).done(function (data) {\r\n    console.log(data.getResponseHeader('Link'));\r\n});<\/code><\/pre>\n<p>But <code>data<\/code> contained just the response body, and <code>data.getResponseHeader<\/code> was null.<\/p>\n<p>A closer look at the jQuery documentation indicated that additional objects are passed to the <em>done<\/em> function, including the jqXHR object. Accessing that object&#8217;s <em>getResponseHeader<\/em> method worked:<\/p>\n<pre><code>$.ajax({\r\n    url:'\/api\/v1\/courses'\r\n}).done(function (data, textStatus, xhr) { \r\n    console.log(xhr.getResponseHeader('Link')); \r\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jQuery&#8217;s ajax method returns a jqXHR object that includes the browser&#8217;s XMLHttpRequest object. Using the getResponseHeader method of this object, you can access properties of the HTTP response header.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[81,309,350,41],"class_list":["post-1103","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-ajax","tag-canvas-api","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=1103"}],"version-history":[{"count":6,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1103\/revisions"}],"predecessor-version":[{"id":1110,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1103\/revisions\/1110"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}