+
+
+
+
+
+
+
// Sorry, no example available.
+
+
+
ADL.XHR_request = function(lrs, url, method, data, auth, callback, callbackargs, ignore404, extraHeaders, withCredentials, strictCallbacks)
+{
+ "use strict";
+
+ var xhr,
+ finished = false,
+ xDomainRequest = false,
+ ieXDomain = false,
+ ieModeRequest,
+ urlparts = url.toLowerCase().match(/^(.+):\/\/([^:\/]*):?(\d+)?(\/.*)?$/),
+ location = window.location,
+ urlPort,
+ result,
+ extended,
+ prop,
+ until;
+
+ //Consolidate headers
+ var headers = {};
+ headers["Content-Type"] = "application/json";
+ headers["Authorization"] = auth;
+ headers['X-Experience-API-Version'] = ADL.XAPIWrapper.xapiVersion;
+ if(extraHeaders !== null){
+ for (var headerName in extraHeaders) {
+ if (extraHeaders.hasOwnProperty(headerName))
+ headers[headerName] = extraHeaders[headerName];
+ }
+ }
+
+ //See if this really is a cross domain
+ xDomainRequest = (location.protocol.toLowerCase() !== urlparts[1] || location.hostname.toLowerCase() !== urlparts[2]);
+ if (!xDomainRequest) {
+ urlPort = (urlparts[3] === null ? ( urlparts[1] === 'http' ? '80' : '443') : urlparts[3]);
+ xDomainRequest = (urlPort === location.port);
+ }
+
+ //Add extended LMS-specified values to the URL
+ if (lrs !== null && lrs.extended !== undefined) {
+ extended = new Array();
+ for (prop in lrs.extended) {
+ extended.push(prop + "=" + encodeURIComponent(lrs.extended[prop]));
+ }
+ if (extended.length > 0) {
+ url += (url.indexOf("?") > -1 ? "&" : "?") + extended.join("&");
+ }
+ }
+
+ //If it's not cross domain or we're not using IE, use the usual XmlHttpRequest
+ var windowsVersionCheck = window.XDomainRequest && (window.XMLHttpRequest && new XMLHttpRequest().responseType === undefined);
+ if (!xDomainRequest || windowsVersionCheck === undefined || windowsVersionCheck===false) {
+ xhr = new XMLHttpRequest();
+ xhr.withCredentials = withCredentials; //allow cross domain cookie based auth
+ xhr.open(method, url, callback != null);
+ for(var headerName in headers){
+ xhr.setRequestHeader(headerName, headers[headerName]);
+ }
+ }
+ //Otherwise, use IE's XDomainRequest object
+ else {
+ ieXDomain = true;
+ ieModeRequest = ie_request(method, url, headers, data);
+ xhr = new XDomainRequest();
+ xhr.open(ieModeRequest.method, ieModeRequest.url);
+ }
+
+ //Setup request callback
+ function requestComplete() {
+ if(!finished){
+ // may be in sync or async mode, using XMLHttpRequest or IE XDomainRequest, onreadystatechange or
+ // onload or both might fire depending upon browser, just covering all bases with event hooks and
+ // using 'finished' flag to avoid triggering events multiple times
+ finished = true;
+ var notFoundOk = (ignore404 && xhr.status === 404);
+ if (xhr.status === undefined || (xhr.status >= 200 && xhr.status < 400) || notFoundOk) {
+ if (callback) {
+ if(callbackargs){
+ strictCallbacks ? callback(null, xhr, callbackargs) : callback(xhr, callbackargs);
+ }
+ else {
+ var body;
+
+ try {
+ body = JSON.parse(xhr.responseText);
+ }
+ catch(e){
+ body = xhr.responseText;
+ }
+
+ strictCallbacks ? callback(null, xhr, body) : callback(xhr,body);
+ }
+ } else {
+ result = xhr;
+ return xhr;
+ }
+ } else {
+ var warning;
+ try {
+ warning = "There was a problem communicating with the Learning Record Store. ( "
+ + xhr.status + " | " + xhr.response+ " )" + url
+ } catch (ex) {
+ warning = ex.toString();
+ }
+ ADL.XAPIWrapper.log(warning);
+ ADL.xhrRequestOnError(xhr, method, url, callback, callbackargs, strictCallbacks);
+ result = xhr;
+ return xhr;
+ }
+ } else {
+ return result;
+ }
+ };
+
+ xhr.onreadystatechange = function () {
+ if (xhr.readyState === 4) {
+ return requestComplete();
+ }
+ };
+
+ xhr.onload = requestComplete;
+ xhr.onerror = requestComplete;
+ //xhr.onerror = ADL.xhrRequestOnError(xhr, method, url);
+
+ xhr.send(ieXDomain ? ieModeRequest.data : data);
+
+ if (!callback) {
+ // synchronous
+ if (ieXDomain) {
+ // synchronous call in IE, with no asynchronous mode available.
+ until = 1000 + new Date();
+ while (new Date() < until && xhr.readyState !== 4 && !finished) {
+ delay();
+ }
+ }
+ return requestComplete();
+ }
+};
+
+