Home | History | Annotate | Download | only in scripts
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 var net = net || {};
     27 
     28 (function () {
     29 
     30 // FIXME: Excise this last bit of jquery ajax code.
     31 // There are callers that depend on automatically parsing the content as JSON or XML
     32 // based off the content-type. Instead we should add net.json and net.xml for those cases.
     33 net.get = function(url, success)
     34 {
     35     $.get(url, success);
     36 };
     37 
     38 net.ajax = function(options)
     39 {
     40     var xhr = new XMLHttpRequest();
     41     var method = options.type || 'GET';
     42     var async = true;
     43     xhr.open(method, options.url, async);
     44     xhr.onload = function() {
     45         if (xhr.status == 200 && options.success)
     46             options.success(xhr.responseText);
     47         else if (xhr.status != 200 && options.error)
     48             options.error();
     49     };
     50     xhr.onerror = function() {
     51         if (options.error)
     52             options.error();
     53     };
     54     var data = options.data || null;
     55     if (data)
     56         xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
     57     xhr.send(data);
     58 };
     59 
     60 net.post = function(url, data, success)
     61 {
     62     net.ajax({
     63         url: url,
     64         type: 'POST',
     65         data: data,
     66         success: success,
     67     });
     68 
     69 };
     70 
     71 net.probe = function(url, options)
     72 {
     73     net.ajax({
     74         url: url,
     75         type: 'HEAD',
     76         success: options.success,
     77         error: options.error,
     78     });
     79 };
     80 
     81 // We use XMLHttpRequest and CORS to fetch JSONP rather than using script tags.
     82 // That's better for security and performance, but we need the server to cooperate
     83 // by setting CORS headers.
     84 net.jsonp = function(url, callback)
     85 {
     86     net.ajax({
     87         url: url,
     88         success: function(jsonp) {
     89             callback(base.parseJSONP(jsonp));
     90         },
     91         error: function() {
     92             callback({});
     93         },
     94     });
     95 };
     96 
     97 })();
     98