refactored PR #30 - Add request method and request data to the base64encode() method
Showing
2 changed files
with
19 additions
and
24 deletions
... | @@ -125,9 +125,9 @@ | ... | @@ -125,9 +125,9 @@ |
125 | * @param String data The data to send, optional | 125 | * @param String data The data to send, optional |
126 | * @return string Base64 encoded result | 126 | * @return string Base64 encoded result |
127 | */ | 127 | */ |
128 | base64encode: function(url,method,data) { | 128 | base64encode: function(url, method, data) { |
129 | return this.evaluate(function(url,method,data) { | 129 | return this.evaluate(function(url, method, data) { |
130 | return __utils__.getBase64(url,method,data); | 130 | return __utils__.getBase64(url, method, data); |
131 | }, { url: url, method: method, data: data }); | 131 | }, { url: url, method: method, data: data }); |
132 | }, | 132 | }, |
133 | 133 | ... | ... |
... | @@ -224,12 +224,12 @@ | ... | @@ -224,12 +224,12 @@ |
224 | * contents. | 224 | * contents. |
225 | * | 225 | * |
226 | * @param String url The resource url | 226 | * @param String url The resource url |
227 | * @param String method The request method, optional | 227 | * @param String method The request method, optional (default: GET) |
228 | * @param Object data The request data, optional | 228 | * @param Object data The request data, optional |
229 | * @return String Base64 contents string | 229 | * @return String Base64 contents string |
230 | */ | 230 | */ |
231 | this.getBase64 = function(url,method,data) { | 231 | this.getBase64 = function(url, method, data) { |
232 | return this.encode(this.getBinary(url,method,data)); | 232 | return this.encode(this.getBinary(url, method, data)); |
233 | }; | 233 | }; |
234 | 234 | ||
235 | /** | 235 | /** |
... | @@ -243,38 +243,33 @@ | ... | @@ -243,38 +243,33 @@ |
243 | */ | 243 | */ |
244 | this.getBinary = function(url, method, data) { | 244 | this.getBinary = function(url, method, data) { |
245 | try { | 245 | try { |
246 | var xhr = new XMLHttpRequest(); | 246 | var xhr = new XMLHttpRequest(), dataString = ""; |
247 | if (method === undefined || ["GET","get","POST","post"].indexOf(method) == -1) { | 247 | if (typeof method !== "string" || ["GET", "POST"].indexOf(method.toUpperCase()) === -1) { |
248 | method = "GET"; | 248 | method = "GET"; |
249 | } else { | 249 | } else { |
250 | method = method.toUpperCase(); | 250 | method = method.toUpperCase(); |
251 | } | 251 | } |
252 | |||
253 | xhr.open(method, url, false); | 252 | xhr.open(method, url, false); |
254 | this.log("using HTTP method: '" + method + "'", "debug"); | 253 | this.log("getBinary(): Using HTTP method: '" + method + "'", "debug"); |
255 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); | 254 | xhr.overrideMimeType("text/plain; charset=x-user-defined"); |
256 | if (method == "POST") { | 255 | if (method === "POST") { |
257 | if(data === undefined) { | 256 | if (typeof data === "object") { |
258 | data_str = ""; | 257 | var dataList = []; |
259 | } else { | 258 | for (var k in data) { |
260 | data_str = ""; | 259 | dataList.push(escape(k) + "=" + escape(data[k].toString())); |
261 | for (k in data) { | ||
262 | if (typeof(k) == "string" && typeof(data[k]) == "string") { | ||
263 | data_str += "&" + escape(k) + "=" + escape(data[k]); | ||
264 | } | ||
265 | } | 260 | } |
266 | data_str = data_str.substring(1); | 261 | dataString = dataList.join('&'); |
262 | this.log("getBinary(): Using request data: '" + dataString + "'", "debug"); | ||
267 | } | 263 | } |
268 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | 264 | xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
269 | } | 265 | } |
270 | this.log("using request data: '" + data_str + "'", "debug"); | 266 | xhr.send(method === "POST" ? dataString : null); |
271 | xhr.send(method == "POST" ? data_str : null); | ||
272 | return xhr.responseText; | 267 | return xhr.responseText; |
273 | } catch (e) { | 268 | } catch (e) { |
274 | if (e.name === "NETWORK_ERR" && e.code === 101) { | 269 | if (e.name === "NETWORK_ERR" && e.code === 101) { |
275 | this.log("unfortunately, casperjs cannot make cross domain ajax requests", "warning"); | 270 | this.log("getBinary(): Unfortunately, casperjs cannot make cross domain ajax requests", "warning"); |
276 | } | 271 | } |
277 | this.log("error while fetching " + url + ": " + e, "error"); | 272 | this.log("getBinary(): Error while fetching " + url + ": " + e, "error"); |
278 | return ""; | 273 | return ""; |
279 | } | 274 | } |
280 | }; | 275 | }; | ... | ... |
-
Please register or sign in to post a comment