93,7 → 93,7 |
* boolean setAsync(optional boolean bAsync = true); |
* // async=bAsync |
* |
* boolean setData(optional string sData = ""); |
* boolean setData(optional (ArrayBufferView or Blob or Document or DOMString or FormData)? sData = null); |
* // data=sData |
* |
* boolean setRequestType( |
100,7 → 100,7 |
* optional string sRequestType = "application/x-www-form-urlencoded"); |
* // requestType=sRequestType |
* |
* boolean send(optional string sData = null, |
* boolean send(optional (ArrayBufferView or Blob or Document or DOMString or FormData)? sData = null, |
* optional string sURL = document.URL, |
* optional string sMethod = HTTPMethod.GET, |
* optional boolean bAsync = true); |
156,6 → 156,7 |
this.setAsync(bAsync); |
this.setSuccessListener(fSuccessListener); |
this.setErrorListener(fErrorListener); |
this.setResponseListener(); |
this.setData(); |
this.setRequestType(); |
} |
265,6 → 266,9 |
|
useCache: true, |
|
_handledSuccess: false, |
_handledError: false, |
|
/** |
* Method to be called onreadystatechange |
* |
271,38 → 275,9 |
* @private |
* @function |
*/ |
_responseListener: (function() { |
var |
Request = jsx.net.http.Request, |
jsx_object = jsx.object, |
oStatus = Request.status; |
_responseListener: void 0, |
|
/** |
* @param {XMLHttpRequest} response |
*/ |
return function (response) { |
if (response.readyState === Request.readyState.COMPLETED) |
{ |
var reqStatus = response.status; |
if (oStatus.OK_EXPR.test(reqStatus)) |
{ |
if (jsx_object.isMethod(this.successListener)) |
{ |
return this.successListener(response); |
} |
} |
else if (oStatus.FAILED_EXPR.test(reqStatus)) |
{ |
if (jsx_object.isMethod(this.errorListener)) |
{ |
this.errorListener(response); |
} |
} |
} |
}; |
}()), |
|
/** |
* Sets the <code>URL</code> property. |
* |
* @param {string} sURL |
463,13 → 438,13 |
/** |
* Sets the <code>data</code> property. |
* |
* @param {string} sData (optional) |
* @param {ArrayBufferView|Blob|Document|string|FormData} sData (optional) |
* If not provided or a false-value, sets |
* the property to the empty string. |
* the property to <code>null</code>. |
* @see HTTPRequest.prototype#resetData() |
*/ |
setData: function (sData) { |
this.data = (sData || ""); |
this.data = (sData || null); |
return this; |
}, |
|
571,7 → 546,8 |
* @return {jsx.net.http.Request} |
* This object |
*/ |
return function (obj) { |
function _setRequestHeaders (obj) |
{ |
for (var name in obj) |
{ |
if (_hasOwnProperty(obj, name)) |
581,7 → 557,9 |
} |
|
return this; |
}; |
} |
|
return _setRequestHeaders; |
}()), |
|
dontCache: function () { |
595,15 → 573,20 |
* a new object if necessary; |
* |
* @protected |
* @function |
*/ |
_getXHR: (function () { |
var jsx_global = jsx.global, |
_isMethod = jsx.object.isMethod; |
|
/** |
* @return {IXMLHttpRequest} |
* A reference to an XML HTTP Request object or <code>null</code>, |
* if no such object can be created. |
*/ |
_getXHR: function () { |
var |
jsx_global = jsx.global, |
jsx_object = jsx.object, |
x = this._xhr; |
function _getXHR () |
{ |
var x = this._xhr; |
|
/* Reuse existing XHR instance if possible */ |
if (x !== null |
627,7 → 610,7 |
* currently not for `file:' URIs, so we don't prefer that wrapper (see |
* <http://xhab.blogspot.com/2006/11/ie7-support-for-xmlhttprequest.html>). |
*/ |
if (jsx_object.isMethod(jsx_global, "ActiveXObject")) |
if (_isMethod(jsx_global, "ActiveXObject")) |
{ |
/*@cc_on @*/ |
/*@if (@_jscript_version >= 5) |
647,7 → 630,7 |
} |
|
/* Gecko and Opera 8.1+ */ |
if (!x && jsx_object.isMethod(jsx_global, "XMLHttpRequest")) |
if (!x && _isMethod(jsx_global, "XMLHttpRequest")) |
{ |
jsx.tryThis( |
function () { x = new XMLHttpRequest(); }, |
656,7 → 639,7 |
|
/* IceBrowser */ |
if (!x && typeof window != "undefined" |
&& jsx_object.isMethod(window, "createRequest")) |
&& _isMethod(window, "createRequest")) |
{ |
jsx.tryThis( |
function () { x = window.createRequest(); }, |
670,17 → 653,26 |
} |
|
return x; |
}, |
} |
|
return _getXHR; |
}()), |
|
/** |
* Submits the HTTP request. |
* |
* @param {string} sData (optional) |
* @function |
*/ |
send: (function () { |
var _isMethod = jsx.object.isMethod; |
|
/** |
* @param {ArrayBufferView|Blob|Document|string|FormData} sData (optional) |
* The data to form the request body. If the request method is "GET", |
* this argument is ignored and <code>null</code> is used instead (no body). |
* If the request method is "POST", and this value is not provided, the |
* value defaults to that of the <code>data</code> property, which is |
* the empty string if not set different previously. |
* <code>null</code> if not set differently before. |
* @param {string} sURL (optional) |
* The request URL. If not provided, this value defaults to that of the |
* <code>URL</code> property, which is the empty string if not set |
702,13 → 694,13 |
* the message, and responded with an OK status code; only that the |
* method could be called successfully. |
*/ |
send: function (sData, sURL, sMethod, bAsync) { |
function _send (sData, sURL, sMethod, bAsync) |
{ |
var |
jsx_object = jsx.object, |
C = this.constructor, |
x = this._getXHR(); |
|
if (!x || !jsx_object.isMethod(x, "open")) |
if (!x || !_isMethod(x, "open")) |
{ |
return false; |
} |
754,7 → 746,7 |
|
var me = this; |
|
if (jsx_object.isMethod(x, "setRequestHeader")) |
if (_isMethod(x, "setRequestHeader")) |
{ |
/* NOTE: Failure to call this method is _not_ considered a fatal error. */ |
jsx.tryThis( |
768,41 → 760,51 |
}); |
} |
|
var _hasOwnProperty = jsx.object._hasOwnProperty; |
var requestHeaders = me.requestHeaders; |
|
for (var name in requestHeaders) |
var names = jsx.object.getKeys(requestHeaders); |
for (var i = 0, len = names.length; i < len; ++i) |
{ |
if (_hasOwnProperty(requestHeaders, name)) |
{ |
var name = names[i]; |
x.setRequestHeader(name, requestHeaders[name]); |
} |
} |
} |
); |
} |
|
this._handledSuccess = this._handledError = false; |
|
if (bAsync) |
{ |
x.onreadystatechange = (function(x2) { |
return function () { |
// alert(x.readyState); |
// alert(x.status); |
if (typeof x.onload != "undefined") |
{ |
x.onload = function (response) { |
if (!this._handledSuccess) |
{ |
return me.successListener(response); |
} |
}; |
} |
|
// console.log("readyState = %i, status = %i", x.readyState, x.status); |
// console.log(C.status.OK_EXPR); |
if (typeof x.onerror != "undefined") |
{ |
x.onerror = function (response) { |
if (!this._handledError) |
{ |
return this.errorListener(response); |
} |
}; |
} |
|
if (jsx_object.isMethod(me._responseListener)) |
x.onreadystatechange = (function (x2) { |
function _onReadyStateChange () |
{ |
if (_isMethod(me._responseListener)) |
{ |
me._responseListener(x2); |
} |
} |
|
/* Let the garbage collector handle this per the closure */ |
// if (x2.readyState == C.readyState.COMPLETED) |
// { |
// x2 = null; |
// } |
}; |
return _onReadyStateChange; |
}(x)); |
} |
|
812,7 → 814,7 |
|
if (!bAsync) |
{ |
if (jsx_object.isMethod(this._responseListener)) |
if (_isMethod(this._responseListener)) |
{ |
result = this._responseListener(x); |
} |
832,6 → 834,9 |
|
return result; |
} |
|
return _send; |
}()) |
}; |
|
/** |
854,7 → 859,39 |
* @constructor |
*/ |
jsx.net.http.ResponseListener = function (sCode) { |
return Function("x", sCode + "" || ""); |
var |
Request = jsx.net.http.Request, |
oStatus = Request.status; |
|
return (sCode |
? Function("x", String(sCode)) |
: /** |
* @param {XMLHttpRequest} x |
*/ |
function (x) { |
if (x.readyState === Request.readyState.COMPLETED) |
{ |
var reqStatus = x.status; |
if (oStatus.OK_EXPR.test(reqStatus)) |
{ |
if (typeof this.successListener == "function" |
&& !this._handledSuccess) |
{ |
this._handledSuccess = true; |
return this.successListener(x); |
} |
} |
else if (oStatus.FAILED_EXPR.test(reqStatus)) |
{ |
if (typeof this.errorListener == "function" |
&& !this._handledError) |
{ |
this._handledError = true; |
this.errorListener(x); |
} |
} |
} |
}); |
}; |
|
/* Usage: */ |