webservice.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5webservice = new (function() { 6 7this.AJAX_BASE_URL_ = '/ajax'; 8 9this.onServerUnreachableOrTimeout = null; 10 11this.ajaxRequest = function(path, responseCallback, errorCallback, postArgs) { 12 var reqType = postArgs ? 'POST' : 'GET'; 13 var reqData = postArgs ? JSON.stringify(postArgs) : ''; 14 15 $.ajax({ 16 url: this.AJAX_BASE_URL_ + path, 17 type: reqType, 18 data: reqData, 19 success: responseCallback, 20 dataType: 'json', 21 error: function (xhr, ajaxOptions, thrownError) { 22 console.log('------------------------'); 23 console.log('AJAX error (req: ' + path + ').'); 24 console.log('HTTP response: ' + xhr.status + ' ' + thrownError); 25 console.log(xhr.responseText); 26 if (errorCallback) 27 errorCallback(xhr.status, xhr.responseText); 28 if (xhr.readyState < 4 && this_.onServerUnreachableOrTimeout != null) 29 webservice.onServerUnreachableOrTimeout(); 30 } 31 }); 32}; 33 34})();