UNPKG

63.9 kBHTMLView Raw
1<!DOCTYPE html>
2
3<html lang="en">
4<head>
5 <meta charset="utf-8">
6 <meta name="viewport" content="width=device-width">
7 <title>CrossBrowdy API documentation Source: CrossBase/net/XHR/CB_Net_XHR.js</title>
8
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
13
14 <link type="text/css" rel="stylesheet" href="styles/site.cosmo.css">
15
16</head>
17
18<body style="min-width:800px; overflow-wrap:break-word; word-wrap:break-word; word-break:break-word; line-break:strict; hyphens:none; -webkit-hyphens:none; -moz-hyphens:none;">
19
20<div class="navbar navbar-default navbar-fixed-top ">
21<div class="container">
22 <div class="navbar-header">
23 <a class="navbar-brand" href="index.html">CrossBrowdy API documentation</a>
24 <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
25 <span class="icon-bar"></span>
26 <span class="icon-bar"></span>
27 <span class="icon-bar"></span>
28 </button>
29 </div>
30 <div class="navbar-collapse collapse" id="topNavigation">
31 <ul class="nav navbar-nav">
32
33 <li class="dropdown">
34 <a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a>
35 <ul class="dropdown-menu inline">
36 <li><a href="CB_Arrays.html">CB_Arrays</a></li><li><a href="CB_AudioDetector.html">CB_AudioDetector</a></li><li><a href="CB_baseSymbols.html">CB_baseSymbols</a></li><li><a href="CB_Client.html">CB_Client</a></li><li><a href="CB_Collisions.html">CB_Collisions</a></li><li><a href="CB_Configuration.html">CB_Configuration</a></li><li><a href="CB_Configuration.CrossBase.html">CB_Configuration.CrossBase</a></li><li><a href="CB_Configuration.CrossBrowdy.html">CB_Configuration.CrossBrowdy</a></li><li><a href="CB_Controllers.html">CB_Controllers</a></li><li><a href="CB_Controllers_Proprietary.html">CB_Controllers_Proprietary</a></li><li><a href="CB_Controllers_Proprietary.WII.html">CB_Controllers_Proprietary.WII</a></li><li><a href="CB_Controllers_Proprietary.WII_U.html">CB_Controllers_Proprietary.WII_U</a></li><li><a href="CB_Device.html">CB_Device</a></li><li><a href="CB_Device.AmbientLight.html">CB_Device.AmbientLight</a></li><li><a href="CB_Device.Battery.html">CB_Device.Battery</a></li><li><a href="CB_Device.Location.html">CB_Device.Location</a></li><li><a href="CB_Device.Motion.html">CB_Device.Motion</a></li><li><a href="CB_Device.Orientation.html">CB_Device.Orientation</a></li><li><a href="CB_Device.Proximity.html">CB_Device.Proximity</a></li><li><a href="CB_Device.Vibration.html">CB_Device.Vibration</a></li><li><a href="CB_Elements.html">CB_Elements</a></li><li><a href="CB_Events.html">CB_Events</a></li><li><a href="CB_Keyboard.html">CB_Keyboard</a></li><li><a href="CB_Keyboard.chars.html">CB_Keyboard.chars</a></li><li><a href="CB_Keyboard.extended.html">CB_Keyboard.extended</a></li><li><a href="CB_Keyboard.keys.html">CB_Keyboard.keys</a></li><li><a href="CB_Modules.html">CB_Modules</a></li><li><a href="CB_Mouse.html">CB_Mouse</a></li><li><a href="CB_Mouse.CursorImage.html">CB_Mouse.CursorImage</a></li><li><a href="CB_Net.html">CB_Net</a></li><li><a href="CB_Net.Fetch.html">CB_Net.Fetch</a></li><li><a href="CB_Net.REST.html">CB_Net.REST</a></li><li><a href="CB_Net.Sockets.html">CB_Net.Sockets</a></li><li><a href="CB_Net.Sockets.SockJS.html">CB_Net.Sockets.SockJS</a></li><li><a href="CB_Net.XHR.html">CB_Net.XHR</a></li><li><a href="CB_Pointer.html">CB_Pointer</a></li><li><a href="CB_Screen.html">CB_Screen</a></li><li><a href="CB_Speaker.html">CB_Speaker</a></li><li><a href="CB_Touch.html">CB_Touch</a></li>
37 </ul>
38 </li>
39
40 <li class="dropdown">
41 <a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
42 <ul class="dropdown-menu inline">
43 <li><a href="CB_AudioFile.html">CB_AudioFile</a></li><li><a href="CB_AudioFile_API.AAPI.html">CB_AudioFile_API.AAPI</a></li><li><a href="CB_AudioFile_API.ACMP.html">CB_AudioFile_API.ACMP</a></li><li><a href="CB_AudioFile_API.SM2.html">CB_AudioFile_API.SM2</a></li><li><a href="CB_AudioFile_API.WAAPI.html">CB_AudioFile_API.WAAPI</a></li><li><a href="CB_AudioFileCache.html">CB_AudioFileCache</a></li><li><a href="CB_AudioFileSprites.html">CB_AudioFileSprites</a></li><li><a href="CB_AudioFileSpritesPool.html">CB_AudioFileSpritesPool</a></li><li><a href="CB_Canvas.html">CB_Canvas</a></li><li><a href="CB_GraphicSprites.html">CB_GraphicSprites</a></li><li><a href="CB_GraphicSpritesScene.html">CB_GraphicSpritesScene</a></li>
44 </ul>
45 </li>
46
47 <li class="dropdown">
48 <a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
49 <ul class="dropdown-menu inline">
50 <li><a href="global.html#CB_addCredits">CB_addCredits</a></li><li><a href="global.html#CB_BASE_NAME">CB_BASE_NAME</a></li><li><a href="global.html#CB_baseToBase">CB_baseToBase</a></li><li><a href="global.html#CB_baseToInt">CB_baseToInt</a></li><li><a href="global.html#CB_br2nl">CB_br2nl</a></li><li><a href="global.html#CB_brToNl">CB_brToNl</a></li><li><a href="global.html#CB_combineArraysOrObjects">CB_combineArraysOrObjects</a></li><li><a href="global.html#CB_combineAutomatically">CB_combineAutomatically</a></li><li><a href="global.html#CB_combineJSON">CB_combineJSON</a></li><li><a href="global.html#CB_combineURIParameters">CB_combineURIParameters</a></li><li><a href="global.html#CB_combineURLParameters">CB_combineURLParameters</a></li><li><a href="global.html#CB_console">CB_console</a></li><li><a href="global.html#CB_copyObject">CB_copyObject</a></li><li><a href="global.html#CB_countDecimalDigits">CB_countDecimalDigits</a></li><li><a href="global.html#CB_countDecimalPart">CB_countDecimalPart</a></li><li><a href="global.html#CB_countDecimals">CB_countDecimals</a></li><li><a href="global.html#CB_countIntegerDigits">CB_countIntegerDigits</a></li><li><a href="global.html#CB_countIntegerPart">CB_countIntegerPart</a></li><li><a href="global.html#CB_credits">CB_credits</a></li><li><a href="global.html#CB_CREDITS_DEFAULT">CB_CREDITS_DEFAULT</a></li><li><a href="global.html#CB_forceString">CB_forceString</a></li><li><a href="global.html#CB_forEach">CB_forEach</a></li><li><a href="global.html#CB_getBase64StringObject">CB_getBase64StringObject</a></li><li><a href="global.html#CB_getCookie">CB_getCookie</a></li><li><a href="global.html#CB_getDatum">CB_getDatum</a></li><li><a href="global.html#CB_getJSONPropertyValue">CB_getJSONPropertyValue</a></li><li><a href="global.html#CB_getLZStringObject">CB_getLZStringObject</a></li><li><a href="global.html#CB_getValueIndex">CB_getValueIndex</a></li><li><a href="global.html#CB_getValuePath">CB_getValuePath</a></li><li><a href="global.html#CB_includeJSFile">CB_includeJSFile</a></li><li><a href="global.html#CB_indexOf">CB_indexOf</a></li><li><a href="global.html#CB_init">CB_init</a></li><li><a href="global.html#CB_intToBase">CB_intToBase</a></li><li><a href="global.html#CB_isArray">CB_isArray</a></li><li><a href="global.html#CB_isEmail">CB_isEmail</a></li><li><a href="global.html#CB_isFileLocal">CB_isFileLocal</a></li><li><a href="global.html#CB_isString">CB_isString</a></li><li><a href="global.html#CB_lastIndexOf">CB_lastIndexOf</a></li><li><a href="global.html#CB_ltrim">CB_ltrim</a></li><li><a href="global.html#CB_NAME">CB_NAME</a></li><li><a href="global.html#CB_nl2br">CB_nl2br</a></li><li><a href="global.html#CB_nlToBr">CB_nlToBr</a></li><li><a href="global.html#CB_numberFormat">CB_numberFormat</a></li><li><a href="global.html#CB_numberOfDecimalDigits">CB_numberOfDecimalDigits</a></li><li><a href="global.html#CB_numberOfDecimals">CB_numberOfDecimals</a></li><li><a href="global.html#CB_numberOfIntegerDigits">CB_numberOfIntegerDigits</a></li><li><a href="global.html#CB_OPTIONS">CB_OPTIONS</a></li><li><a href="global.html#CB_parseJSON">CB_parseJSON</a></li><li><a href="global.html#CB_parseString">CB_parseString</a></li><li><a href="global.html#CB_regularExpressionString">CB_regularExpressionString</a></li><li><a href="global.html#CB_renderString">CB_renderString</a></li><li><a href="global.html#CB_replaceAll">CB_replaceAll</a></li><li><a href="global.html#CB_rtrim">CB_rtrim</a></li><li><a href="global.html#CB_scriptPath">CB_scriptPath</a></li><li><a href="global.html#CB_scriptPathCalculate">CB_scriptPathCalculate</a></li><li><a href="global.html#CB_setCookie">CB_setCookie</a></li><li><a href="global.html#CB_setDatum">CB_setDatum</a></li><li><a href="global.html#CB_sizeof">CB_sizeof</a></li><li><a href="global.html#CB_sizeOf">CB_sizeOf</a></li><li><a href="global.html#CB_stringifyJSON">CB_stringifyJSON</a></li><li><a href="global.html#CB_symmetricCall">CB_symmetricCall</a></li><li><a href="global.html#CB_symmetricCallClear">CB_symmetricCallClear</a></li><li><a href="global.html#CB_this">CB_this</a></li><li><a href="global.html#CB_trim">CB_trim</a></li><li><a href="global.html#CB_VERSION">CB_VERSION</a></li>
51 </ul>
52 </li>
53
54 </ul>
55
56 <div class="col-sm-3 col-md-3">
57 <form class="navbar-form" role="search">
58 <div class="input-group">
59 <input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
60 <div class="input-group-btn">
61 <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
62 </div>
63 </div>
64 </form>
65 </div>
66
67 </div>
68
69</div>
70</div>
71
72
73<div class="container" id="toc-content" style="width:100%;">
74<div class="row" style="width:100%;">
75
76
77 <div class="col-md-12">
78
79 <div id="main">
80
81
82 <h1 class="page-title">Source: CrossBase/net/XHR/CB_Net_XHR.js</h1>
83
84<section>
85 <article>
86 <pre
87 class="sunlight-highlight-javascript linenums">/**
88 * @file [XMLHttpRequest]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}, [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)}) and related management. Contains the {@link CB_Net.XHR} static class.
89 * @author Joan Alba Maldonado &lt;workindalian@gmail.com>
90 * @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}.
91 */
92
93
94if (typeof(CB_Net) === "undefined") { var CB_Net = function() { return CB_Net; }; }
95
96/**
97 * Static class to manage [XMLHttpRequest]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}, [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)}) and related. It will return itself if it is tried to be instantiated.
98 * @namespace
99 * @todo Think about providing an easy way to abort XHR (AJAX) calls.
100 */
101CB_Net.XHR = function() { return CB_Net.XHR; };
102{
103 CB_Net.XHR.initialized = false; //It will tells whether the object has been initialized or not.
104
105
106 //Initializes all values:
107 CB_Net.XHR.init = function()
108 {
109 if (CB_Net.XHR.initialized) { return CB_Net.XHR; }
110
111 //The object has been initialized:
112 CB_Net.XHR.initialized = true;
113
114 //TODO.
115
116 return CB_Net.XHR;
117 }
118
119
120 CB_Net.XHR._supportedReturnCache = null;
121 /**
122 * Returns whether [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) is available or not.
123 * @function
124 * @returns {boolean} Returns true if [XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} ([AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)}) objects can be used.
125 */
126 CB_Net.XHR.supported = function()
127 {
128 if (CB_Net.XHR._supportedReturnCache !== null) { return CB_Net.XHR._supportedReturnCache; }
129 else
130 {
131 CB_Net.XHR._supportedReturnCache = (CB_Net.XHR.get() !== null);
132 return CB_Net.XHR._supportedReturnCache;
133 }
134 }
135
136
137 //Returns an AJAX object:
138 //var CB_Net.XHR._getXmlHttpVersionsIE = ["Msxml2.XMLHTTP.7.0", "MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
139 CB_Net.XHR._getXmlHttpVersionsIE = ["MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //XmlHttpVersions in order of preference for old IE versions.
140 CB_Net.XHR._getXmlHttpVersionsIELastIndexWorked = null; //Defines the last index of CB_XmlHttpVersion that worked (for optimization).
141 /**
142 * Returns a new [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) object, if possible.
143 * @function
144 * @returns {Object|null} Returns a new [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) object if it has been possible to create it or null otherwise.
145 */
146 CB_Net.XHR.get = function()
147 {
148 if (typeof XMLHttpRequest !== "undefined") //if (window.XMLHttpRequest).
149 {
150 return new XMLHttpRequest();
151 }
152 else if (typeof(ActiveXObject) !== "undefined")
153 {
154 if (CB_Net.XHR._getXmlHttpVersionsIELastIndexWorked !== null)
155 {
156 return new ActiveXObject(CB_Net.XHR._getXmlHttpVersionsIE[CB_Net.XHR._getXmlHttpVersionsIELastIndexWorked]);
157 }
158 else
159 {
160 var XHR = null;
161 for (var x = 0, getXmlHttpVersionsIELength = CB_Net.XHR._getXmlHttpVersionsIE.length; x &lt; getXmlHttpVersionsIELength; x++)
162 {
163 try
164 {
165 XHR = new ActiveXObject(CB_Net.XHR._getXmlHttpVersionsIE[x]);
166 CB_Net.XHR._getXmlHttpVersionsIELastIndexWorked = x; //Defines this index as the last working one (for optimization).
167 return XHR;
168 }
169 catch(E) {}
170 }
171 return null;
172 }
173 }
174 return null;
175 }
176
177
178 /**
179 * Object containing the [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} and their values.
180 * @memberof CB_Net.XHR
181 * @typedef {Object} CB_Net.XHR.HEADERS
182 * @property {Object} HTTPHeaderName - Each property name is an [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} and its value is the desired one for this header.
183 * @example { "Content-Type" : "text/plain; charset=x-user-defined", "Cache-Control" : "no-cache", "Pragma" : "no-cache" }
184 */
185
186
187 /**
188 * Performs an [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) call.
189 * @function
190 * @param {string} URL - The URL that we want to call. It can also contain URL (GET) parameters.
191 * @param {string} [method='POST'] - The [HTTP method]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods} that will be used to perform the call (GET, POST, PUT, DELETE, etc.).
192 * @param {string|Object} [data] - The data that we want to send. If a string is given and "GET" method is being used, it will assume they are GET (URL) parameters and will be attached at the end of the URL given. If something which is not a string is given, it will assume it is a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object and will try to convert it into a string (using the [JSON.stringify]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify} function internally) before sending it.
193 * @param {CB_Net.XHR.HEADERS} [headers] - Object containing the [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} names and their values that we want to send (used internally by the [setRequestHeader]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader} method of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}).
194 * @param {string} [responseType] - If provided, it will be used for the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} property of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} (if available).
195 * @param {string} [mimeType] - [MIME type]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types} that will be used to override the default one returned by the server. Only used when the client supports the [overrideMimeType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/overrideMimeType} method of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}.
196 * @param {function} [callbackFunction] - Function that will be used for the [onreadystatechange]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange} property of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}. The unique parameter that it will receive is the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} used by the request. If provided, the "callbackFunctionOK" and "callbackFunctionError" parameters will not be used even they were also provided.
197 * @param {function} [callbackFunctionOK] - Function that will be called by an internally-created function used in the [onreadystatechange]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange} property of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} when the [readyState]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState} property is 4 and the [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property is included in the "allowedSuccessStatuses" desired. The first parameter it will receive is the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} used by the request and the second one will be the "callbackFunctionError" function provided (if any). It will not be used if the parameter "callbackFunction" is provided.
198 * @param {function} [callbackFunctionError] - Function that will be called by an internally-created function used in the [onreadystatechange]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange} property of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} when the [readyState]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState} property is 4 and the [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property is not included in the "allowedSuccessStatuses" desired. The first parameter it will receive is the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} used by the request and the second one will be the "callbackFunctionOk" function provided (if any). It will not be used if the parameter "callbackFunction" is provided.
199 * @param {integer|array} [allowedSuccessStatuses=200] - An integer or a numeric array with a list of integers with the status or statuses that will be considered as a success call by the "callbackFunctionOK" function (only when it is used) when the response comes.
200 * @param {boolean} [asynchronous=true] - Defines whether to make a request asynchronously or not. It will be used for the third parameter of the method [open]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open} of the [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}.
201 * @param {Object} [XHR] - The [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} that we want to use for the call. If not provided, it will try to create a new one internally.
202 * @returns {Object|null} Returns null if the URL provided was empty or the [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) object provided is not a valid object or it could not be created a new one internally. Otherwise, it returns the [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) object used to try to perform the call (even that maybe it failed or will fail later).
203 * @todo Think about providing a way to choose whether we want the "data" provided to be added to the URL when the "GET" method is used or not.
204 * @todo Describe better what kind of [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} will the different callbacks receive, as in some cases (as when using [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer}) they can contain some special properties with [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers}, etc.
205 */
206 CB_Net.XHR.call = function(URL, method, data, headers, responseType, mimeType, callbackFunction, callbackFunctionOK, callbackFunctionError, allowedSuccessStatuses, asynchronous, XHR)
207 {
208 var failFunction = function(XHR)
209 {
210 if (typeof(callbackFunction) === "function") { callbackFunction.call(XHR, XHR); }
211 else if (typeof(callbackFunctionError) === "function") { callbackFunctionError.call(XHR, XHR, callbackFunctionOK); }
212 };
213
214 //If the URL is empty, exits the function:
215 URL = CB_trim(URL);
216 if (URL === "")
217 {
218 failFunction({ readyState: 4, status: 400, errorMessage: "URL is empty", originalXHR: XHR });
219 return null;
220 }
221
222 //If not given, sets the default parameters:
223 method = CB_trim(method).toUpperCase();
224 if (method === "") { method = "POST"; } //Request method by default.
225
226 if (typeof(headers) !== "object" || headers === null) //Sets headers by default:
227 {
228 headers = {};
229 }
230 mimeType = CB_trim(mimeType); //If it was unset or null, it will be an empty string.
231 responseType = CB_trim(responseType).toLowerCase(); //If it was unset or null, it will be an empty string.
232 if (asynchronous !== false) { asynchronous = true; } //Async by default.
233
234 //By default, allows 200 success status only:
235 if (!CB_isArray(allowedSuccessStatuses)) { allowedSuccessStatuses = [200]; }
236
237 //Creates the AJAX object:
238 if (typeof(XHR) === "undefined" || XHR === null || !XHR) { XHR = CB_Net.XHR.get(); }
239
240 //If the XHR object is null, exits the function:
241 if (XHR === null)
242 {
243 failFunction({ readyState: 4, status: 400, errorMessage: "XHR is null", originalXHR: XHR });
244 return null;
245 }
246
247 //Processes the data (if any):
248 if (typeof(data) !== "undefined" &amp;&amp; data !== null &amp;&amp; !CB_isString(data)) //If data is not a string, we assume it is JSON data.
249 {
250 data = JSON.stringify(data); //data = CB_Net.URLEncode(JSON.stringify(data));
251 }
252 else
253 {
254 data = CB_trim(data);
255
256 //If there are data and the method is GET, it adds them to the URL:
257 if (data !== "" &amp;&amp; method === "GET")
258 {
259 if (CB_indexOf(URL, "?") === -1) { URL += "?" + data; } //There was not ? symbol in the URL, so we add it.
260 else { URL += "&amp;" + data; } //There was ? symbol in the URL, so we add the &amp; symbol.
261 //TODO: consider setting "data" to null or empty string since we do not need them to send them in the "send" method (we have already added them in the "URL" variable).
262 }
263 }
264
265 //Opens the connection:
266 try //Using try-catch to avoid problems with IE5.5:
267 {
268 XHR.open(method, URL, asynchronous);
269 }
270 catch(E)
271 {
272 var originalXHR = XHR;
273 var fakeXHR = { readyState: 4, status: 500, errorMessage: "Failed when using open method", originalXHR: XHR };
274 try
275 {
276 XHR.readyState = 4;
277 XHR.status = 500;
278 XHR.errorMessage = "Failed when using open method";
279 } catch(E) { XHR = fakeXHR; }
280 if (XHR.readyState !== 4) { XHR = fakeXHR; } //Some web clients as IE9 will not fail when try to edit properties but they will not end being modified.
281 failFunction(XHR);
282 return originalXHR;
283 }
284
285 //Applies the given headers (if any):
286 //if (mimeType !== null) { headers["Content-Type"] = mimeType; }
287 for (var headerName in headers)
288 {
289 XHR.setRequestHeader(headerName, headers[headerName]);
290 }
291
292 //Applies the given mime type (if any):
293 if (XHR.overrideMimeType &amp;&amp; mimeType !== "")
294 {
295 //XHR.overrideMimeType("text/plain; charset=UTF-8");
296 XHR.overrideMimeType(mimeType);
297 }
298
299 //Applies the given response type (if any):
300 if (typeof(XHR.responseType) !== "undefined" &amp;&amp; responseType !== "")
301 {
302 XHR.responseType = responseType;
303 }
304
305 /*
306 else
307 {
308 if (responseType === "" || responseType === "text")
309 {
310 XHR.overrideMimeType("text/plain; charset=UTF-8");
311 }
312 else if (responseType === "xml")
313 {
314 XHR.overrideMimeType("text/xml");
315 }
316 else if (responseType === "arraybuffer")
317 {
318 //XHR.overrideMimeType("text/plain; charset=x-user-defined");
319 }
320 }*/
321
322 //If set, defines the callback function:
323 if (typeof(callbackFunction) === "function")
324 {
325 XHR.onreadystatechange = function() { callbackFunction.call(XHR, XHR); };
326 }
327 //...otherwise, defines the callback functions for OK and error status:
328 else
329 {
330 XHR.onreadystatechange = function()
331 {
332 //if (XHR.readyState == 4)
333 if (XHR.readyState === 4)
334 {
335 //if (XHR.status == 200 || allowOtherSuccessStatus &amp;&amp; XHR.status == 206)
336 if (CB_indexOf(allowedSuccessStatuses, parseInt(XHR.status)) !== -1)
337 {
338 if (typeof(callbackFunctionOK) === "function")
339 {
340 callbackFunctionOK.call(XHR, XHR, callbackFunctionError);
341 }
342 }
343 //else if (XHR.readyState == 4 &amp;&amp; (XHR.status == 0 || XHR.status == 502 || XHR.status == 12002 || XHR.status == 12029 || XHR.status == 12030 || XHR.status == 12031 || XHR.status == 12029 || XHR.status == 12152 || XHR.status == 12159))
344 //else if (XHR.status !== 12152 &amp;&amp; XHR.status !== 12030 &amp;&amp; XHR.status !== 0 &amp;&amp; XHR.status !== 12002 &amp;&amp; XHR.status !== 12007 &amp;&amp; XHR.status !== 12029 &amp;&amp; XHR.status !== 12031)
345 else
346 {
347 if (typeof(callbackFunctionError) === "function")
348 {
349 callbackFunctionError.call(XHR, XHR, callbackFunctionOK);
350 }
351 }
352 }
353 }
354 }
355
356 //Sends the XHR request:
357 XHR.send(data);
358
359 //Returns the XHR object:
360 return XHR;
361 }
362
363
364 /**
365 * Performs a standard [XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} request to send form data by POST (no files). Uses the {@link CB_Net.XHR.call} function internally with "POST" method, asynchronously and "mimeType" parameter not provided.
366 * @function
367 * @param {string} URL - Used for the "URL" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
368 * @param {string|Object} [data] - Used for the "data" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
369 * @param {CB_Net.XHR.HEADERS} [headers={ "Content-Type" : "application/x-www-form-urlencoded; charset=" + charset, "Cache-Control" : "no-cache", "Pragma" : "no-cache" }] - Object containing the [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} names and their values that we want to send. If not provided, it will use the default one that will include the charset defined by the "charset" parameter. An empty object ({}) can be used if we do not want to send any [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} at all. Used for the "headers" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
370 * @param {string} [responseType] - Used for the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} parameter of the {@link CB_Net.XHR.call} function when it is called internally.
371 * @param {string} [charset='UTF-8'] - The charset for the "Content-Type" [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} that will be sent by default only when no "headers" parameter is provided.
372 * @param {function} [callbackFunction] - Used for the "callbackFunction" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
373 * @param {function} [callbackFunctionOK] - Used for the "callbackFunctionOK" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
374 * @param {function} [callbackFunctionError] - Used for the "callbackFunctionError" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
375 * @param {integer|array} [allowedSuccessStatuses=200] - Used for the "allowedSuccessStatuses" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
376 * @param {Object} [XHR] - Used for the "XHR" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
377 * @returns {Object|null} Returns the same that the {@link CB_Net.XHR.call} function returns (called internally).
378 */
379 CB_Net.XHR.callForm = function(URL, data, headers, responseType, charset, callbackFunction, callbackFunctionOK, callbackFunctionError, allowedSuccessStatuses, XHR)
380 {
381 //If not given, sets the default parameters:
382 charset = CB_trim(charset);
383 if (typeof(charset) === "undefined" || charset === "") { charset = "UTF-8"; } //Default charset.
384 if (typeof(headers) !== "object" || headers === null)
385 {
386 headers =
387 {
388 "Content-Type" : "application/x-www-form-urlencoded; charset=" + charset,
389 "Cache-Control" : "no-cache",
390 "Pragma" : "no-cache"
391 };
392 }
393
394 //Makes the AJAX request function and returns the same:
395 return CB_Net.XHR.call
396 (
397 URL, //URL
398 "POST", //method
399 data, //data
400 headers, //headers
401 responseType, //responseType
402 null, //mimeType
403 callbackFunction, //callbackFunction
404 callbackFunctionOK, //callbackFunctionOK
405 callbackFunctionError, //callbackFunctionError
406 allowedSuccessStatuses, //allowedSuccessStatuses
407 true, //asynchronous
408 XHR //XHR object
409 );
410 }
411
412
413 /**
414 * Performs a standard [XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} request for a binary file. Uses the {@link CB_Net.XHR.call} function internally with "GET" method, using "text/plain; charset=x-user-defined" for the "mimeType" parameter and asynchronously.
415 * @function
416 * @param {string} URL - Used for the "URL" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
417 * @param {string|Object} [data] - Used for the "data" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
418 * @param {CB_Net.XHR.HEADERS} [headers={ "Content-Type" : "text/plain; charset=x-user-defined", "Cache-Control" : "no-cache", "Pragma" : "no-cache" }] - Object containing the [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} names and their values that we want to send. If not provided, it will use the default one. An empty object ({}) can be used if we do not want to send any [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} at all. Used for the "headers" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
419 * @param {'arraybuffer'|'blob'|''} [blobOrArrayBuffer='arraybuffer'] - Used for the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} parameter of the {@link CB_Net.XHR.call} function when it is called internally. If an empty string is provided, the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} parameter will not be modified.
420 * @param {function} [callbackFunction] - Used for the "callbackFunction" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
421 * @param {function} [callbackFunctionOK] - Used for the "callbackFunctionOK" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
422 * @param {function} [callbackFunctionError] - Used for the "callbackFunctionError" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
423 * @param {integer|array} [allowedSuccessStatuses=200] - Used for the "allowedSuccessStatuses" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
424 * @param {Object} [XHR] - Used for the "XHR" parameter of the {@link CB_Net.XHR.call} function when it is called internally.
425 * @returns {Object|null} Returns the same that the {@link CB_Net.XHR.call} function returns (called internally).
426 */
427 CB_Net.XHR.callBinary = function(URL, data, headers, blobOrArrayBuffer, callbackFunction, callbackFunctionOK, callbackFunctionError, allowedSuccessStatuses, XHR)
428 {
429 //If not given, sets the default parameters:
430 if (typeof(headers) !== "object" || headers === null)
431 {
432 headers =
433 {
434 "Content-Type" : "text/plain; charset=x-user-defined",
435 "Cache-Control" : "no-cache",
436 "Pragma" : "no-cache"
437 };
438 }
439 blobOrArrayBuffer = typeof(blobOrArrayBuffer) === "undefined" || blobOrArrayBuffer === null ? "arraybuffer" : CB_trim(blobOrArrayBuffer).toLowerCase();
440 if (blobOrArrayBuffer !== "blob" &amp;&amp; blobOrArrayBuffer !== "") //Only allows to be "blob", "arraybuffer" or an empty string:
441 {
442 blobOrArrayBuffer = "arraybuffer";
443 }
444
445 //Makes the AJAX request function and returns the same:
446 return CB_Net.XHR.call
447 (
448 URL, //URL
449 "GET", //method
450 data, //data
451 headers, //headers
452 blobOrArrayBuffer, //responseType
453 "text/plain; charset=x-user-defined", //mimeType
454 callbackFunction, //callbackFunction
455 callbackFunctionOK, //callbackFunctionOK
456 callbackFunctionError, //callbackFunctionError
457 allowedSuccessStatuses, //allowedSuccessStatuses
458 true, //asynchronous
459 XHR //XHR object
460 );
461 }
462
463
464 /**
465 * Performs an [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)} ([XHR]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest}) call through the proxy (made with [PHP]{@link https://en.wikipedia.org/wiki/PHP} language and using [cURL]{@link https://en.wikipedia.org/wiki/CURL}, so it will need a server which supports that) to avoid [cross-domain request]{@link https://en.wikipedia.org/wiki/Cross-origin_resource_sharing} limitations of [AJAX]{@link https://en.wikipedia.org/wiki/Ajax_(programming)}. Uses the {@link CB_Net.XHR.callForm} function (with "headers" and "charset" parameters not provided) internally to call the proxy.
466 &lt;br />
467 NOTE: Edit the "CB_proxy.config.php" file to configure the default proxy (set by default in the value of the {@link CB_Configuration.CrossBase.CB_Net_XHR_PROXY_URL} property). Apart from configuring it, adding some security measures is highly recommended.
468 Have in mind that, for safety reasons, the default proxy only allows to request the URLs defined in the "$allowedURLs" array in the "CB_proxy.config.php" file. Just edit it to allow other URLs.
469 * @function
470 * @param {string} URL - The URL that we want the proxy to call for us. It can also contain URL (GET) parameters.
471 * @param {string} [method='POST'] - The [HTTP method]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods} (GET, POST, PUT, DELETE, etc.) that we want the proxy to use for us when performing the call.
472 * @param {string|Object} [data] - The data that we want to send through the proxy to the final server. If something which is not a string is given, it will assume it is a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object and will try to convert it into a string (using the [JSON.stringify]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify} function internally) before sending it.
473 * @param {CB_Net.XHR.HEADERS} [headers] - Object ([JSON]{@link https://en.wikipedia.org/wiki/JSON} format) containing the [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} names and their values that we want the proxy to send to the final server. Even if not provided, the proxy could end sending some [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} depending on the [cURL]{@link https://en.wikipedia.org/wiki/CURL} configuration used.
474 * @param {string} [responseType] - Used for the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
475 * @param {boolean} [forceJSON=false] - If it is set to true, the response from the proxy will be a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object with the [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property containing the [HTTP status]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status} of the reply, the [response]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response} property with the response content itself and the "headers" property (only when "getHeaders" parameters is set to true) with the [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} of the reply, all belonging to the response from the final server.
476 * @param {boolean} [getHeaders=false] - If it is set to true, the proxy will answer including the [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} from the final server. The [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} will be included in the final response string at the beginning (before the response content) if the "forceJSON" parameter is not set to true or in the "headers" property of the [JSON]{@link https://en.wikipedia.org/wiki/JSON} object that belongs to the response otherwise.
477 * @param {boolean} [headersForceOneDimension=false] - If it is set to true, the proxy will consider that the [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} of the response from the final server are not multidimensional which means that the final server would never reply the same [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} repeated multiple times (with different values, normally) in different chunks separated by double new lines ("\r\n\r\n"). Default value (false) is recommended. Needs "getHeaders" set to true.
478 * @param {boolean} [headersForceOneDimensionValues=false] - If it is set to true, the proxy will only consider one value per [HTTP header]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} from the response from the final server. Default value (false) is recommended. Needs "getHeaders" set to true.
479 * @param {boolean} [transparentStatus=false] - If it is set to true, the proxy will reply us with the same status as the final server in its HTTP response.
480 * @param {boolean} [transparentHeaders=false] - If it is set to true, the proxy will reply us with the same [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} as the final server in its HTTP response.
481 * @param {function} [callbackFunction] - Used for the "callbackFunction" parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
482 * @param {function} [callbackFunctionOK] - Used for the "callbackFunctionOK" parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
483 * @param {function} [callbackFunctionError] - Used for the "callbackFunctionError" parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
484 * @param {integer|array} [allowedSuccessStatuses=200] - Used for the "allowedSuccessStatuses" parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
485 * @param {Object} [XHR] - Used for the "XHR" parameter of the {@link CB_Net.XHR.callForm} function when it is called internally.
486 * @returns {Object|null} Returns the same that the {@link CB_Net.XHR.callForm} function returns (called internally).
487 * @todo Document PHP proxy more.
488 */
489 CB_Net.XHR.callProxy = function(URL, method, data, headers, responseType, forceJSON, getHeaders, headersForceOneDimension, headersForceOneDimensionValues, transparentStatus, transparentHeaders, callbackFunction, callbackFunctionOK, callbackFunctionError, allowedSuccessStatuses, XHR)
490 {
491 //Adds the proxy data and session (for users without cookies enabled) to the data:
492 var proxyVariables = "p_url=" + CB_Net.URLValueEncode(URL);
493 method = CB_trim(method).toUpperCase();
494 if (method === "") { method = "POST"; } //Request method by default.
495 proxyVariables += "&amp;p_method=" + CB_Net.URLValueEncode(method);
496 if (typeof(headers) === "object" &amp;&amp; headers !== null) { proxyVariables += "&amp;p_headers=" + CB_Net.URLValueEncode(JSON.stringify(headers)); }
497 if (forceJSON) { proxyVariables += "&amp;p_force_json=yes"; }
498 if (getHeaders) { proxyVariables += "&amp;p_get_headers=yes"; }
499 if (headersForceOneDimension) { proxyVariables += "&amp;p_get_headers_on_dimension=yes"; }
500 if (headersForceOneDimensionValues) { proxyVariables += "&amp;p_get_headers_on_dimension_values=yes"; }
501 if (transparentStatus) { proxyVariables += "&amp;p_transparent_status=yes"; }
502 if (transparentHeaders) { proxyVariables += "&amp;p_transparent_headers=yes"; }
503
504 //proxyVariables += "&amp;" + SESSION_PARAMETER;
505 //proxyVariables += "&amp;ajax=true";
506 //proxyVariables += "&amp;php_root=" + PROJECT_PATH_FROM_PROXY;
507 if (typeof(data) !== "undefined" &amp;&amp; data !== null &amp;&amp; !CB_isString(data)) //If data is not a string, we assume it is JSON data.
508 {
509 data = JSON.stringify(data);
510 }
511 else { data = CB_trim(data); }
512 if (data !== "") { data = "p_data=" + CB_Net.URLValueEncode(data) + "&amp;" + proxyVariables; }
513 else { data = proxyVariables; }
514
515 //Calls the proxy by post method:
516 return CB_Net.XHR.callForm
517 (
518 CB_Configuration[CB_BASE_NAME].CB_Net_XHR_PROXY_URL, //URL of the proxy.
519 data, //data
520 null, //headers
521 responseType, //responseType
522 null, //charset
523 callbackFunction, //callbackFunction
524 callbackFunctionOK, //callbackFunctionOK
525 callbackFunctionError, //callbackFunctionError
526 allowedSuccessStatuses, //allowedSuccessStatuses
527 XHR //XHR
528 );
529 }
530
531
532 /**
533 * Performs a [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer} call to a [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer} server. Uses the {@link CB_Net.XHR.callProxy} function (without "callbackFunction" parameter provided) internally if we do not want to avoid the proxy or uses the {@link CB_Net.XHR.call} function (asynchronously, with "mimeType" and "callbackFunction" parameters not provided) otherwise.
534 * @function
535 * @param {string} [serverURL={@link CB_Net.REST.SERVER_URL_DEFAULT}] - The URL of the [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer} server that we want to call. It should not contain URL (GET) parameters. It can also contain the [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer} path (route), although it is recommended to set it in the "route" parameter.
536 * @param {string} [route] - The [REST]{@link https://en.wikipedia.org/wiki/Representational_state_transfer} path (route) we want to request. It can also contain URL (GET) parameters, although it is recommended to set them in the "dataURL" parameter.
537 * @param {string} [dataURL] - The URL (GET) data that we want to send.
538 * @param {string} [method='POST'] - Used for the "method" parameter for the {@link CB_Net.XHR.callProxy} function or for the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
539 * @param {string|Object} [data] - Used for the "data" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
540 * @param {CB_Net.XHR.HEADERS} [headers=undefined|{ "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8", "Cache-Control" : "no-cache", "Pragma" : "no-cache" }] - Used for the "headers" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally. If not provided and the {@link CB_Net.XHR.call} function is used (if no proxy is allowed), the default value will be: { "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8", "Cache-Control" : "no-cache", "Pragma" : "no-cache" }
541 * @param {string} [responseType] - Used for the [responseType]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType} parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
542 * @param {boolean} [avoidProxy=false] - If it is set to true, it will call the {@link CB_Net.XHR.call} internally. Otherwise, it will use the {@link CB_Net.XHR.callProxy} function internally.
543 * @param {boolean} [forceJSON=false] - Used for the "forceJSON" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
544 * @param {boolean} [getHeaders=false] - Used for the "getHeaders" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
545 * @param {boolean} [headersForceOneDimension=false] - Used for the "headersForceOneDimension" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
546 * @param {boolean} [headersForceOneDimensionValues=false] - Used for the "headersForceOneDimensionValues" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
547 * @param {boolean} [transparentStatus=false] - Used for the "transparentStatus" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
548 * @param {boolean} [transparentHeaders=false] - Used for the "transparentHeaders" parameter of the {@link CB_Net.XHR.callProxy} function (if the proxy is allowed) when it is called internally.
549 * @param {function} [callbackFunctionOK] - Used for the "callbackFunctionOK" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
550 * @param {function} [callbackFunctionError] - Used for the "callbackFunctionError" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
551 * @param {integer|array} [allowedSuccessStatuses=200] - Used for the "allowedSuccessStatuses" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
552 * @param {Object} [XHR] - Used for the "XHR" parameter of the {@link CB_Net.XHR.callProxy} function or of the {@link CB_Net.XHR.call} function (if no proxy is allowed) when it is called internally.
553 * @returns {Object|null} When using the proxy is allowed, returns the same that the {@link CB_Net.XHR.callProxy} function returns (called internally). Otherwise, it returns the same that the {@link CB_Net.XHR.call} function returns (called internally).
554 */
555 CB_Net.XHR.callREST = function(serverURL, route, dataURL, method, data, headers, responseType, avoidProxy, forceJSON, getHeaders, headersForceOneDimension, headersForceOneDimensionValues, transparentStatus, transparentHeaders, callbackFunctionOK, callbackFunctionError, allowedSuccessStatuses, XHR)
556 {
557 serverURL = CB_trim(serverURL);
558 //if (serverURL === "") { serverURL = CB_trim(CB_Net.REST.SERVER_URL_DEFAULT); }
559 if (serverURL === "") { serverURL = CB_Net.REST.SERVER_URL_DEFAULT; }
560 serverURL += CB_trim(route);
561
562 dataURL = CB_trim(dataURL);
563 if (dataURL !== "")
564 {
565 serverURL = CB_rtrim(serverURL, ["?", "&amp;", " "]);
566 if (CB_indexOf(serverURL, "?") !== -1) { serverURL += "&amp;" + dataURL; }
567 else { serverURL += "?" + dataURL; }
568 }
569
570 if (!avoidProxy)
571 {
572 return CB_Net.XHR.callProxy
573 (
574 serverURL, //URL of the REST server.
575 method, //method
576 data, //data
577 headers, //headers
578 responseType, //responseType
579 forceJSON, //forceJSON
580 getHeaders, //getHeaders
581 headersForceOneDimension, //headersForceOneDimension
582 headersForceOneDimensionValues, //headersForceOneDimensionValues
583 transparentStatus, //transparentStatus
584 transparentHeaders, //transparentHeaders
585 null, //callbackFunction
586 callbackFunctionOK, //callbackFunctionOK
587 callbackFunctionError, //callbackFunctionError
588 allowedSuccessStatuses, //allowedSuccessStatuses
589 XHR //XHR
590 );
591 }
592 else
593 {
594 if (typeof(headers) !== "object" || headers === null)
595 {
596 headers =
597 {
598 "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
599 "Cache-Control" : "no-cache",
600 "Pragma" : "no-cache"
601 };
602 }
603
604 //Makes the AJAX request function and returns the same:
605 return CB_Net.XHR.call
606 (
607 serverURL, //URL
608 method, //method
609 data, //data
610 headers, //headers
611 responseType, //responseType
612 null, //mimeType
613 null, //callbackFunction
614 callbackFunctionOK, //callbackFunctionOK
615 callbackFunctionError, //callbackFunctionError
616 allowedSuccessStatuses, //allowedSuccessStatuses
617 true, //asynchronous
618 XHR //XHR object
619 );
620 }
621 }
622
623
624 /**
625 * Returns the [HTTP headers]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers} (it should be an object) from the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy ("headers" property). Useful to parse the response from the {@link CB_Net.XHR.callProxy} (or {@link CB_Net.XHR.callREST} and related) function when it has been called with the "forceJSON" parameter set to true.
626 * @function
627 * @param {Object|string} response - The [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} which contains the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property or the value of the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property (string) itself which should contain the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy. If a string is provided, tries to parse it as a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object. The "headers" property will be tried to be returned from it.
628 * @param {boolean} [sanitize=true] - If it is set to true and the "headers" property is not found, it will return an empty object ({}) instead of returning null.
629 * @returns {Object|null} Returns the content of the "headers" property if possible (it should be a {@link CB_Net.XHR.HEADERS} object). If it is not possible, it will return null if the parameter "sanitize" is set to false or an empty object ({}) otherwise.
630 * @todo Consider adding the parameter "headerNameFirst".
631 */
632 CB_Net.XHR.getResponseHeaders = function(response, sanitize) //TODO: Add parameter headerNameFirst
633 {
634 if (typeof(sanitize) === "undefined" || sanitize === null) { sanitize = true; } //Sanitizes by default.
635 var headers = CB_getJSONPropertyValue(response.responseText ? response.responseText : response, "headers", null);
636 if (sanitize &amp;&amp; headers === null) { return {}; } //If the property is not found or null and we want to sanitize, returns an empty object by default.
637 return headers; //Can be NULL.
638 }
639
640
641 /**
642 * Returns the [HTTP status]{@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status} code from an [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} ([status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property) or from the info array generated by the [PHP]{@link https://en.wikipedia.org/wiki/PHP}'s [curl_getinfo]{@link http://php.net/manual/function.curl-getinfo.php} function ("http_code" index) or from the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy ([status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property). Useful to parse the response from the {@link CB_Net.XHR.callProxy} (or {@link CB_Net.XHR.callREST} and related) function when it has been called with the "forceJSON" parameter set to true.
643 * @function
644 * @param {Object|string} response - The [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} which contains the [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property or the value of the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property (string) itself which should contain the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy. If a string is provided, tries to parse it as a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object. The [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} property will be tried to be returned from it (or the "http_code" property as a fallback).
645 * @param {boolean} [sanitize=true] - If it is set to true and neither the "status" nor the "http_code" property are found (or is not a number), it will return the value of "statusDefault" instead of returning null.
646 * @param {boolean} [statusDefault=-1] - Default value to return when the status cannot be found (or is not a number). Only used when the "sanitize" parameter is set to true.
647 * @returns {integer|*} Returns the content of the [status]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status} (or "http_code") property if possible (it should be an integer). If it is not possible, it will return null if the parameter "sanitize" is set to false or the value of "statusDefault" otherwise.
648 */
649 CB_Net.XHR.getStatusCode = function(response, statusDefault, sanitize)
650 {
651 if (typeof(sanitize) === "undefined" || sanitize === null) { sanitize = true; } //Sanitizes by default.
652 if (typeof(statusDefault) === "undefined" || statusDefault === null) { statusDefault = -1; }
653 var statusCode = CB_getJSONPropertyValue(response, "status", null);
654 if (statusCode === null || isNaN(statusCode)) { statusCode = CB_getJSONPropertyValue(response, "http_code", null); }
655 if (sanitize &amp;&amp; (statusCode === null || isNaN(statusCode))) { return statusDefault; } //If the property is not found or null and we want to sanitize, returns -1 by default.
656 return !isNaN(statusCode) ? parseInt(statusCode) : null; //Can be NULL.
657 }
658
659
660 /**
661 * Returns the response content from an [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} (from its [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property) or from the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy ([response]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response} property). Useful to parse the response from the {@link CB_Net.XHR.callProxy} (or {@link CB_Net.XHR.callREST} and related) function when it has been called with the "forceJSON" parameter set to true.
662 * @function
663 * @param {Object|string} response - The [XHR object]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest} which contains the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property or the value of the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property (string) itself which should contain the [JSON]{@link https://en.wikipedia.org/wiki/JSON} response generated by the "getSslPage" function used by the [PHP]{@link https://en.wikipedia.org/wiki/PHP} proxy. If a string is provided, tries to parse it as a [JSON]{@link https://en.wikipedia.org/wiki/JSON} object. The [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} property (or the [response]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response} property as a fallback) will be tried to be returned from it.
664 * @param {boolean} [sanitize=true] - If it is set to true and neither the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} nor the [response]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response} properties are found, it will return an empty string ("") instead of returning null.
665 * @returns {string|null} Returns the content of the [responseText]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText} (or [response]{@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response}) property if possible. If it is not possible, it will return null if the parameter "sanitize" is set to false or an empty string ("") otherwise.
666 */
667 CB_Net.XHR.getResponseContent = function(responseOrXHR, sanitize)
668 {
669 if (typeof(sanitize) === "undefined" || sanitize === null) { sanitize = true; } //Sanitizes by default.
670 var response = null;
671 try //Prevents the "InvalidStateError: responseText is only available if responseType is '' or 'text'." error in Firefox.
672 {
673 response = CB_getJSONPropertyValue(responseOrXHR, "responseText", responseOrXHR);
674 }
675 catch(E) {}
676 response = CB_getJSONPropertyValue(response, "response", response);
677 if (sanitize &amp;&amp; response === null) { return ""; } //If the property is not found or null and we want to sanitize, returns an empty string by default.
678 return response ? response : null;
679 }
680
681}</pre>
682 </article>
683</section>
684
685
686
687
688
689 </div>
690 </div>
691
692 <div class="clearfix"></div>
693
694
695
696</div>
697</div>
698
699
700 <div class="modal fade" id="searchResults">
701 <div class="modal-dialog">
702 <div class="modal-content">
703 <div class="modal-header">
704 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
705 <h4 class="modal-title">Search results</h4>
706 </div>
707 <div class="modal-body"></div>
708 <div class="modal-footer">
709 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
710 </div>
711 </div><!-- /.modal-content -->
712 </div><!-- /.modal-dialog -->
713 </div>
714
715
716<footer>
717
718
719 <span class="copyright">
720 <a href="printable/" target="_blank">See a more printer-friendly version</a><hr /><span style="color:#000000">© <address style="display:inline; font-style:normal;"><a href="https://crossbrowdy.com/" target="_blank">CrossBrowdy</a> API documentation</address> by <a href="https://joanalbamaldonado.com/" target="_blank">Joan Alba Maldonado</a> - <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International</a><br />DocStrap Copyright © 2012-2015 The contributors to the JSDoc3 and DocStrap projects.</span>
721 </span>
722
723<span class="jsdoc-message">
724 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
725
726 on Mon Feb 3rd 2020
727
728 using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
729</span>
730</footer>
731
732<script src="scripts/docstrap.lib.js"></script>
733<script src="scripts/toc.js"></script>
734
735 <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
736
737
738<script>
739$( function () {
740 $( "[id*='$']" ).each( function () {
741 var $this = $( this );
742
743 $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
744 } );
745
746 $( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
747 var $this = $( this );
748
749 var example = $this.find( "code" );
750 exampleText = example.html();
751 var lang = /{@lang (.*?)}/.exec( exampleText );
752 if ( lang && lang[1] ) {
753 exampleText = exampleText.replace( lang[0], "" );
754 example.html( exampleText );
755 lang = lang[1];
756 } else {
757 var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
758 lang = langClassMatch ? langClassMatch[1] : "javascript";
759 }
760
761 if ( lang ) {
762
763 $this
764 .addClass( "sunlight-highlight-" + lang )
765 .addClass( "linenums" )
766 .html( example.html() );
767
768 }
769 } );
770
771 Sunlight.highlightAll( {
772 lineNumbers : true,
773 showMenu : true,
774 enableDoclinks : true
775 } );
776
777 $.catchAnchorLinks( {
778 navbarOffset: 10
779 } );
780 $( "#toc" ).toc( {
781 anchorName : function ( i, heading, prefix ) {
782 return $( heading ).attr( "id" ) || ( prefix + i );
783 },
784 selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
785 showAndHide : false,
786 smoothScrolling: true
787 } );
788
789 $( "#main span[id^='toc']" ).addClass( "toc-shim" );
790 $( '.dropdown-toggle' ).dropdown();
791
792 $( "table" ).each( function () {
793 var $this = $( this );
794 $this.addClass('table');
795 } );
796
797} );
798</script>
799
800
801
802<!--Navigation and Symbol Display-->
803
804<script>
805 $( function () {
806 $( '#main' ).localScroll( {
807 offset : { top : 60 } //offset by the height of your header (give or take a few px, see what works for you)
808 } );
809 $( "dt.name" ).each( function () {
810 var $this = $( this ).find("h4");
811 var icon = $( "<i/>" ).addClass( "icon-plus-sign" ).addClass( "pull-right" ).addClass( "icon-white" );
812 var dt = $(this);
813 var children = dt.next( "dd" );
814
815 dt.prepend( icon ).css( {cursor : "pointer"} );
816 dt.addClass( "member-collapsed" ).addClass( "member" );
817
818
819 children.hide();
820
821 dt.children().on( "click", function () {
822 children = dt.next( "dd" );
823 children.slideToggle( "fast", function () {
824
825 if ( children.is( ":visible" ) ) {
826 icon.addClass( "icon-minus-sign" ).removeClass( "icon-plus-sign" ).removeClass( "icon-white" );
827 dt.addClass( "member-open" ).animate( "member-collapsed" );
828 } else {
829 icon.addClass( "icon-plus-sign" ).removeClass( "icon-minus-sign" ).addClass( "icon-white" );
830 dt.addClass( "member-collapsed" ).removeClass( "member-open" );
831 }
832 } );
833 } );
834
835 } );
836 } );
837</script>
838
839
840<!--Google Analytics-->
841
842
843
844 <script type="text/javascript">
845 $(document).ready(function() {
846 SearcherDisplay.init();
847 });
848 </script>
849
850
851</body>
852</html>