1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Extend $tw.utils.httpRequest() to cope with binary data

The problem was that `this.responseText` crashes for non-text data. We
fix it by letting the client specify which property should be returned.

@ericshulman does this work for you?
This commit is contained in:
Jermolene 2017-03-17 13:41:17 +00:00
parent 595072b2bc
commit 73e1724fdf

View File

@ -17,10 +17,12 @@ A quick and dirty HTTP function; to be refactored later. Options are:
url: URL to retrieve url: URL to retrieve
type: GET, PUT, POST etc type: GET, PUT, POST etc
callback: function invoked with (err,data) callback: function invoked with (err,data)
returnProp: string name of the property to return as first argument of callback
*/ */
exports.httpRequest = function(options) { exports.httpRequest = function(options) {
var type = options.type || "GET", var type = options.type || "GET",
headers = options.headers || {accept: "application/json"}, headers = options.headers || {accept: "application/json"},
returnProp = options.returnProp || "responseText",
request = new XMLHttpRequest(), request = new XMLHttpRequest(),
data = "", data = "",
f,results; f,results;
@ -41,7 +43,7 @@ exports.httpRequest = function(options) {
if(this.readyState === 4) { if(this.readyState === 4) {
if(this.status === 200 || this.status === 201 || this.status === 204) { if(this.status === 200 || this.status === 201 || this.status === 204) {
// Success! // Success!
options.callback(null,this.responseText,this); options.callback(null,this[returnProp],this);
return; return;
} }
// Something went wrong // Something went wrong