| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Wrapper around asynchronous HTTP requests. |
| 7 */ |
| 8 |
| 9 /** |
| 10 * Namespace for HTTP related functions. |
| 11 */ |
| 12 var http = {}; |
| 13 |
| 14 /** |
| 15 * Convert a dictionary to form suitable for HTTP POST requests. |
| 16 * @param {dict} data A string -> string dict with parameters for the request. |
| 17 * @return {string} An HTTP POST encoding of data. |
| 18 */ |
| 19 http.encodeFormData = function(data) { |
| 20 var pairs = []; |
| 21 var regexp = /%20/g; |
| 22 |
| 23 for (var name in data) { |
| 24 var value = data[name].toString(); |
| 25 var pair = encodeURIComponent(name).replace(regexp, '+') + '=' + |
| 26 encodeURIComponent(value).replace(regexp, '+'); |
| 27 pairs.push(pair); |
| 28 } |
| 29 |
| 30 return pairs.join('&'); |
| 31 }; |
| 32 |
| 33 /* |
| 34 * Make an asynchronous HTTP POST request. |
| 35 * @param {string} url The url to request. |
| 36 * @param {dict} values A dict of string -> string containing arguments to the |
| 37 * request. |
| 38 * @param {function} callback A function to call with the result of the request. |
| 39 * @param {function} errorHandler A function to call in the event of error. |
| 40 */ |
| 41 http.post = function(url, values, callback, errorHandler) { |
| 42 var request = new XMLHttpRequest(); |
| 43 request.onreadystatechange = function() { |
| 44 if (request.readyState == 4) { |
| 45 if (request.status == 200) { |
| 46 callback(request.responseText); |
| 47 } else { |
| 48 if (errorHandler != null) { |
| 49 errorHandler(request.status, request.statusText); |
| 50 } else { |
| 51 callback(null); |
| 52 } |
| 53 } |
| 54 } |
| 55 }; |
| 56 request.open('POST', url); |
| 57 request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
| 58 request.send(http.encodeFormData(values)); |
| 59 }; |
| 60 |
| 61 /* |
| 62 * Make an asynchronous HTTP GET request. |
| 63 * @param {string} url The url to request. |
| 64 * @param {function} callback A function to call with the result of the request. |
| 65 */ |
| 66 http.getText = function(url, callback) { |
| 67 var request = new XMLHttpRequest(); |
| 68 request.onreadystatechange = function() { |
| 69 if (request.readyState == 4) { |
| 70 if (request.status == 200) { |
| 71 callback(request.responseText); |
| 72 } else { |
| 73 callback(null); |
| 74 } |
| 75 } |
| 76 } |
| 77 request.open('GET', url); |
| 78 request.send(null); |
| 79 }; |
| OLD | NEW |