Last active
May 7, 2021 13:14
-
-
Save Artistan/c8d9d439c70117c8b9dd3e9bd8822d2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* | |
* jquery.binarytransport.js | |
* | |
* @description. jQuery ajax transport for making binary data type requests. | |
* @version 1.0 | |
* @author Henry Algus <[email protected]> | |
* | |
*/ | |
// use this transport for "binary" data type | |
// http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/ | |
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR){ | |
// check for conditions and support for blob / arraybuffer response type | |
if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) | |
{ | |
return { | |
// create new XMLHttpRequest | |
send: function(headers, callback){ | |
// setup all variables | |
var xhr = new XMLHttpRequest(), | |
url = options.url, | |
type = options.type, | |
async = options.async || true, | |
// blob or arraybuffer. Default is blob | |
dataType = options.responseType || "blob", | |
data = options.data || null, | |
username = options.username || null, | |
password = options.password || null; | |
xhr.addEventListener('load', function(){ | |
var data = {}; | |
data[options.dataType] = xhr.response; | |
// make callback and send data | |
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders()); | |
}); | |
xhr.open(type, url, async, username, password); | |
// Apply custom fields if provided | |
if ( options.xhrFields ) { | |
for ( i in options.xhrFields ) { | |
xhr[ i ] = options.xhrFields[ i ]; | |
} | |
} | |
// Override mime type if needed | |
if ( options.mimeType && xhr.overrideMimeType ) { | |
xhr.overrideMimeType( options.mimeType ); | |
} | |
// X-Requested-With header | |
// For cross-domain requests, seeing as conditions for a preflight are | |
// akin to a jigsaw puzzle, we simply never set it to be sure. | |
// (it can always be set on a per-request basis or even using ajaxSetup) | |
// For same-domain requests, won't change header if already provided. | |
if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) { | |
headers[ "X-Requested-With" ] = "XMLHttpRequest"; | |
} | |
// setup custom headers | |
for (var i in headers ) { | |
xhr.setRequestHeader(i, headers[i] ); | |
} | |
xhr.responseType = dataType; | |
xhr.send(data); | |
}, | |
abort: function(){ | |
jqXHR.abort(); | |
} | |
}; | |
} | |
}); |
This file contains hidden or 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
$.ajax({ | |
url: myUrl, | |
type: 'post', | |
headers: { | |
"Accept" : "application/x-list", | |
"Content-Type": "text/plain; charset=utf-8" | |
}, | |
dataType:'binary', | |
data: { | |
'53570': 'cf21e88ba4dc07bfa2b02ed8770f1403f2ca9dc4' | |
}, | |
cache: false, | |
crossDomain: true, | |
xhrFields: { | |
withCredentials: true | |
}, | |
success: function (data, status) { | |
var t = parseResponse(data); | |
console.log("Success!!"); | |
console.log(t); | |
}, | |
error: function (xhr, desc, err) { | |
console.log(xhr); | |
console.log("Desc: " + desc + "\nErr:" + err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment