Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1554)

Unified Diff: tryconsole/static/http.js

Issue 517046: Initial check-in of tryconsole. (Closed) Base URL: svn://chrome-svn/chrome/trunk/tools/
Patch Set: '' Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tryconsole/static/green.png ('k') | tryconsole/static/magenta.png » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tryconsole/static/http.js
===================================================================
--- tryconsole/static/http.js (revision 0)
+++ tryconsole/static/http.js (revision 0)
@@ -0,0 +1,79 @@
+// Copyright (c) 2009-2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview Wrapper around asynchronous HTTP requests.
+ */
+
+/**
+ * Namespace for HTTP related functions.
+ */
+var http = {};
+
+/**
+ * Convert a dictionary to form suitable for HTTP POST requests.
+ * @param {dict} data A string -> string dict with parameters for the request.
+ * @return {string} An HTTP POST encoding of data.
+ */
+http.encodeFormData = function(data) {
+ var pairs = [];
+ var regexp = /%20/g;
+
+ for (var name in data) {
+ var value = data[name].toString();
+ var pair = encodeURIComponent(name).replace(regexp, '+') + '=' +
+ encodeURIComponent(value).replace(regexp, '+');
+ pairs.push(pair);
+ }
+
+ return pairs.join('&');
+};
+
+/*
+ * Make an asynchronous HTTP POST request.
+ * @param {string} url The url to request.
+ * @param {dict} values A dict of string -> string containing arguments to the
+ * request.
+ * @param {function} callback A function to call with the result of the request.
+ * @param {function} errorHandler A function to call in the event of error.
+ */
+http.post = function(url, values, callback, errorHandler) {
+ var request = new XMLHttpRequest();
+ request.onreadystatechange = function() {
+ if (request.readyState == 4) {
+ if (request.status == 200) {
+ callback(request.responseText);
+ } else {
+ if (errorHandler != null) {
+ errorHandler(request.status, request.statusText);
+ } else {
+ callback(null);
+ }
+ }
+ }
+ };
+ request.open('POST', url);
+ request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ request.send(http.encodeFormData(values));
+};
+
+/*
+ * Make an asynchronous HTTP GET request.
+ * @param {string} url The url to request.
+ * @param {function} callback A function to call with the result of the request.
+ */
+http.getText = function(url, callback) {
+ var request = new XMLHttpRequest();
+ request.onreadystatechange = function() {
+ if (request.readyState == 4) {
+ if (request.status == 200) {
+ callback(request.responseText);
+ } else {
+ callback(null);
+ }
+ }
+ }
+ request.open('GET', url);
+ request.send(null);
+};
Property changes on: tryconsole/static/http.js
___________________________________________________________________
Added: svn:mergeinfo
« no previous file with comments | « tryconsole/static/green.png ('k') | tryconsole/static/magenta.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698