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