| OLD | NEW |
| (Empty) |
| 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 <script> | |
| 27 | |
| 28 var net = net || {}; | |
| 29 | |
| 30 (function () { | |
| 31 | |
| 32 net.json = function(url) | |
| 33 { | |
| 34 return net.ajax({ | |
| 35 url: url, | |
| 36 parse: function(xhr) { | |
| 37 return JSON.parse(xhr.responseText); | |
| 38 } | |
| 39 }); | |
| 40 }; | |
| 41 | |
| 42 net.xml = function(url) | |
| 43 { | |
| 44 return net.ajax({ | |
| 45 url: url, | |
| 46 parse: function(xhr) { | |
| 47 return xhr.responseXML; | |
| 48 } | |
| 49 }); | |
| 50 }; | |
| 51 | |
| 52 net.ajax = function(options) | |
| 53 { | |
| 54 return new Promise(function(resolve, reject) { | |
| 55 var xhr = new XMLHttpRequest(); | |
| 56 var method = options.type || 'GET'; | |
| 57 var async = true; | |
| 58 xhr.open(method, options.url, async); | |
| 59 xhr.onload = function() { | |
| 60 if (xhr.status == 200) { | |
| 61 if (options.parse) | |
| 62 resolve(options.parse(xhr)); | |
| 63 else | |
| 64 resolve(xhr.responseText); | |
| 65 } | |
| 66 else if (xhr.status != 200) | |
| 67 reject(Error(xhr.statusText)); | |
| 68 }; | |
| 69 xhr.onerror = function(error) { | |
| 70 reject(error); | |
| 71 }; | |
| 72 var data = options.data || null; | |
| 73 if (data) | |
| 74 xhr.setRequestHeader("content-type","application/x-www-form-urlencod
ed"); | |
| 75 xhr.send(data); | |
| 76 }); | |
| 77 }; | |
| 78 | |
| 79 net.probe = function(url) | |
| 80 { | |
| 81 return net.ajax({ | |
| 82 url: url, | |
| 83 type: 'HEAD', | |
| 84 }); | |
| 85 }; | |
| 86 | |
| 87 net._parseJSONP = function(jsonp) | |
| 88 { | |
| 89 if (!jsonp) | |
| 90 return {}; | |
| 91 | |
| 92 if (!jsonp.match(/^[^{[]*\(/)) | |
| 93 return JSON.parse(jsonp); | |
| 94 | |
| 95 var startIndex = jsonp.indexOf('(') + 1; | |
| 96 var endIndex = jsonp.lastIndexOf(')'); | |
| 97 if (startIndex == 0 || endIndex == -1) | |
| 98 return {}; | |
| 99 return JSON.parse(jsonp.substr(startIndex, endIndex - startIndex)); | |
| 100 }; | |
| 101 | |
| 102 // We use XMLHttpRequest and CORS to fetch JSONP rather than using script tags. | |
| 103 // That's better for security and performance, but we need the server to coopera
te | |
| 104 // by setting CORS headers. | |
| 105 net.jsonp = function(url) | |
| 106 { | |
| 107 return net.ajax({ | |
| 108 url: url, | |
| 109 }).then(function(jsonp) { | |
| 110 return net._parseJSONP(jsonp); | |
| 111 }).catch(function(error) { | |
| 112 return {}; | |
| 113 }); | |
| 114 }; | |
| 115 | |
| 116 })(); | |
| 117 | |
| 118 </script> | |
| OLD | NEW |