| 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
|
|
|
|
|