Created
March 10, 2014 21:44
-
-
Save cvan/9475049 to your computer and use it in GitHub Desktop.
patched requests.js for headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/hearth/media/js/requests.js b/hearth/media/js/requests.js | |
index a959070..f6f6743 100644 | |
--- a/hearth/media/js/requests.js | |
+++ b/hearth/media/js/requests.js | |
@@ -26,7 +26,7 @@ define('requests', | |
} | |
} | |
- function _ajax(type, url, data) { | |
+ function _ajax(type, url, data, headers) { | |
var xhr = new XMLHttpRequest(); | |
var def = defer.Deferred(); | |
@@ -66,7 +66,10 @@ define('requests', | |
xhr.open(type, url, true); | |
+ | |
+ headers = headers || {}; | |
var content_type = 'application/x-www-form-urlencoded'; | |
+ | |
if (data) { | |
if (_is_obj(data) && !_has_object_props(data)) { | |
data = utils.urlencode(data); | |
@@ -77,15 +80,21 @@ define('requests', | |
data = data.toString(); | |
content_type = 'text/plain'; | |
} | |
- xhr.setRequestHeader('Content-Type', content_type); | |
+ headers['Content-Type'] = headers['Content-Type'] || content_type; | |
} | |
+ | |
+ Object.keys(headers).forEach(function(header) { | |
+ console.error(header, headers[header]) | |
+ xhr.setRequestHeader(header, headers[header]); | |
+ }); | |
+ | |
xhr.send(data || undefined); | |
return def.promise(xhr); | |
} | |
- function ajax(type, url, data) { | |
- var def = _ajax(type, url, data); | |
+ function ajax(type, url, data, headers) { | |
+ var def = _ajax(type, url, data, headers); | |
// then() returns a new promise, so don't return that. | |
def.then(function(resp, xhr) { | |
callHooks('success', [resp, xhr, type, url, data]); | |
@@ -138,30 +147,30 @@ define('requests', | |
} | |
} | |
- function del(url) { | |
+ function del(url, data, headers) { | |
console.log('DELETing', url); | |
- return ajax('DELETE', url).done(function() { | |
+ return ajax('DELETE', url, data, headers).done(function() { | |
console.log('DELETEd', url); | |
}); | |
} | |
- function patch(url, data) { | |
+ function patch(url, data, headers) { | |
console.log('PATCHing', url); | |
- return ajax('PATCH', url, data).done(function() { | |
+ return ajax('PATCH', url, data, headers).done(function() { | |
console.log('PATCHed', url); | |
}); | |
} | |
- function post(url, data) { | |
+ function post(url, data, headers) { | |
console.log('POSTing', url); | |
- return ajax('POST', url, data).done(function() { | |
+ return ajax('POST', url, data, headers).done(function() { | |
console.log('POSTed', url); | |
}); | |
} | |
- function put(url, data) { | |
+ function put(url, data, headers) { | |
console.log('PUTing', url); | |
- return ajax('PUT', url, data).done(function() { | |
+ return ajax('PUT', url, data, headers).done(function() { | |
console.log('PUT', url); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment