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

Include configuration headers #249

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,18 @@ then simply place this snippet at the top of your code:
$.ajax.compat && $.ender({ ajax: $.ajax.compat });
```

### Configuration headers ###
Headers that are common for all requests.
```js
reqwest.headers.common = {
"Accept-Language" : "es"
};
```
Header defaults for GET request
```js
reqwest.headers.get = {
"Accept-Language" : "en"
};
```

**Happy Ajaxing!**
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"async",
"sync"
],
"version": "2.0.5",
"version": "2.0.6",
"homepage": "https://github.com/ded/reqwest",
"author": "Dustin Diaz <[email protected]> (http://dustindiaz.com)",
"repository": {
Expand Down
38 changes: 35 additions & 3 deletions reqwest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

var context = this

if ('window' in context) {
if ('document' in context) {
var doc = document
, byTag = 'getElementsByTagName'
, head = doc[byTag]('head')[0]
Expand All @@ -25,7 +25,6 @@
}
}


var httpsRe = /^http/
, protocolRe = /(^\w+):\/\//
, twoHundo = /^(20\d|1223)$/ //http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
Expand Down Expand Up @@ -65,6 +64,11 @@
if (xhr && 'withCredentials' in xhr) {
return xhr
} else if (context[xDomainRequest]) {
var protocolRegExp = /^https?/;
if (window.location.href.match(protocolRegExp)[0] !== o.url.match(protocolRegExp)[0]) {
throw new Error('XDomainRequest: requests must be targeted to the same scheme as the hosting page.')
// As per: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
}
return new XDomainRequest()
} else {
throw new Error('Browser does not support cross-origin requests')
Expand Down Expand Up @@ -106,8 +110,11 @@

function setHeaders(http, o) {
var headers = o['headers'] || {}
, method = (o['method'] || 'get').toLowerCase()
, h

headers = includeDefaultHeaders(headers, method);

headers['Accept'] = headers['Accept']
|| defaultHeaders['accept'][o['type']]
|| defaultHeaders['accept']['*']
Expand Down Expand Up @@ -196,7 +203,7 @@

function getRequest(fn, err) {
var o = this.o
, method = (o['method'] || 'GET').toUpperCase()
, method = o['method'] = (o['method'] || 'GET').toUpperCase()
, url = typeof o === 'string' ? o : o['url']
// convert non-string objects to query-string form unless o['processData'] is false
, data = (o['processData'] !== false && o['data'] && typeof o['data'] !== 'string')
Expand Down Expand Up @@ -626,5 +633,30 @@
}
}

// headers that are common for all requests
reqwest.headers = { common : {}};

function isEmpty(value) {
return (value || '').trim().length === 0
}

function includeDefaultHeaders(headers, method) {
var common;
var methodHeaders = reqwest.headers[method] || {};
for (common in reqwest.headers.common) {
if (!isEmpty(common) && !(common in headers) && !(common in methodHeaders)) {
headers[common] = reqwest.headers.common[common];
}
}

var option;
for (option in methodHeaders) {
if (!isEmpty(option) && !(option in headers)) {
headers[option] = methodHeaders[option];
}
}
return headers;
}

return reqwest
});
Loading