Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add headers option #917

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ var infScroll = new InfiniteScroll( '.container', {
// this.on( 'append', function() {...})
// }

headers: undefined,
// set specific headers on each request
// header: function() {
// return [
// ["key", value]
// ]
// }

debug: false,
// Logs events and state changes to the console.
})
Expand Down
14 changes: 10 additions & 4 deletions dist/infinite-scroll.pkgd.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ proto.updateGetPathTemplate = function( optPath ) {
}.bind( this );
// get pageIndex from location
// convert path option into regex to look for pattern in location
// escape query (?) in url, allows for parsing GET parameters
// escape query (?) in url, allows for parsing GET parameters
var regexString = optPath
.replace( /(\\\?|\?)/, '\\?' )
.replace( '{{#}}', '(\\d\\d?\\d?)' );
Expand Down Expand Up @@ -1037,7 +1037,7 @@ proto.loadNextPage = function() {
this.lastPageReached( response, path );
}.bind( this );

request( path, this.options.responseType, onLoad, onError, onLast );
request( path, this.options.headers, this.options.responseType, onLoad, onError, onLast );
this.dispatchEvent( 'request', null, [ path ] );
};

Expand Down Expand Up @@ -1235,7 +1235,7 @@ proto.stopPrefill = function() {

// -------------------------- request -------------------------- //

function request( url, responseType, onLoad, onError, onLast ) {
function request(url, headers, responseType, onLoad, onError, onLast ) {
var req = new XMLHttpRequest();
req.open( 'GET', url, true );
// set responseType document to return DOM
Expand All @@ -1244,6 +1244,12 @@ function request( url, responseType, onLoad, onError, onLast ) {
// set X-Requested-With header to check that is ajax request
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

if (headers && typeof(headers) === "function") {
headers().forEach(function (header) {
req.setRequestHeader([header[0]], header[1]);
});
}

req.onload = function() {
if ( req.status == 200 ) {
onLoad( req.response );
Expand Down Expand Up @@ -1717,7 +1723,7 @@ return InfiniteScroll;
);
}

}( window, function factory( window, InfiniteScroll, utils ) {
}( window, function factory( window, InfiniteScroll, utils ) {

var proto = InfiniteScroll.prototype;

Expand Down
Loading