|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// ajax_request_helper.js: Ajax request helper |
| 3 | +// This file is a part of pk-engine-js library |
| 4 | +// Copyright (c) Alexander Gladysh <[email protected]> |
| 5 | +// Copyright (c) Dmitry Potapov <[email protected]> |
| 6 | +// See file `COPYRIGHT` for the license |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | + |
| 9 | +PKEngine.check_namespace("Ajax"); |
| 10 | + |
| 11 | +PKEngine.Ajax.PRINT_RECEIVED_DATA = false; |
| 12 | + |
| 13 | +PKEngine.Ajax.do_request = function(url, type, post_data, event_maker, on_error, on_server_conn_error) |
| 14 | +{ |
| 15 | + //console.log("Ajax url:", url) |
| 16 | + //console.log(window.printStackTrace().join("\n")) |
| 17 | + |
| 18 | + on_error = on_error || PKEngine.Ajax.default_error_handler; |
| 19 | + |
| 20 | + $.ajax( |
| 21 | + url, |
| 22 | + { |
| 23 | + 'type' : type, |
| 24 | + 'data' : post_data, |
| 25 | + //'dataType' : "json", // TODO: Hack (non-working, BTW), to be removed |
| 26 | + 'success' : function(data_in, textStatus, jqXHR) |
| 27 | + { |
| 28 | + if (PKEngine.Ajax.PRINT_RECEIVED_DATA) |
| 29 | + { |
| 30 | + console.log("[Ajax.do_request]: " + url, PK.clone(data_in), textStatus, jqXHR) |
| 31 | + } |
| 32 | + |
| 33 | +// assert( |
| 34 | +// typeof(data_in) == "object", |
| 35 | +// I18N('Invalid format of server response for request "${1}"', url) |
| 36 | +// ) |
| 37 | + // TODO: Hack, to be removed |
| 38 | + var data; |
| 39 | + try |
| 40 | + { |
| 41 | + data = (typeof(data_in) == "string") ? JSON.parse(data_in) : data_in; |
| 42 | + } |
| 43 | + catch (ex) |
| 44 | + { |
| 45 | + CRITICAL_ERROR(I18N('Unable to parse server response for request "${1}"', url)); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if(data) |
| 50 | + { |
| 51 | + if(event_maker) |
| 52 | + { |
| 53 | + PKEngine.EventQueue.push(event_maker(data)); |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + on_error(url, textStatus, jqXHR); |
| 59 | + } |
| 60 | + }, |
| 61 | + 'error' : function(jqXHR, textStatus, errorThrown) |
| 62 | + { |
| 63 | + if ( |
| 64 | + textStatus == "timeout" |
| 65 | + || (jqXHR.readyState == 0 && jqXHR.responseText == "") |
| 66 | + || jqXHR.status == 500 || jqXHR.status == 502 |
| 67 | + || jqXHR.status == 503 || jqXHR.status == 504 |
| 68 | + ) |
| 69 | + { |
| 70 | + LOG("ServerConnectionError = " + JSON.stringify(jqXHR)); |
| 71 | + if (on_server_conn_error) |
| 72 | + { |
| 73 | + on_server_conn_error(); |
| 74 | + } |
| 75 | + return; |
| 76 | + } |
| 77 | + on_error(url, textStatus, jqXHR); |
| 78 | + } |
| 79 | + } |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +PKEngine.Ajax.default_error_handler = function(name, textStatus, jqXHR) |
| 84 | +{ |
| 85 | + var loc_text_status = I18N("Ajax error NULL") |
| 86 | + switch (textStatus) |
| 87 | + { |
| 88 | + case 'timeout' : loc_text_status = I18N("Ajax error TIMEOUT"); break; |
| 89 | + case 'error' : loc_text_status = I18N("Ajax ERROR"); break; |
| 90 | + case 'abort' : loc_text_status = I18N("Ajax error ABORT"); break; |
| 91 | + case 'parsererror' : loc_text_status = I18N("Ajax error PARSERERROR"); break; |
| 92 | + } |
| 93 | + |
| 94 | + CRITICAL_ERROR( |
| 95 | + I18N("Bad server answer!") + "<br>" |
| 96 | + + I18N("Request URL: ${1}", name) + "<br>" |
| 97 | + + I18N("Text error: ${1}", loc_text_status) + "<br>" |
| 98 | + + I18N("Response status: ${1}", jqXHR.status) + "<br>" |
| 99 | + + I18N("Response text: ${1}", jqXHR.responseText) |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | + |
| 104 | +PKEngine.Ajax.on_soft_error_received = function(name, error) |
| 105 | +{ |
| 106 | + assert(error) |
| 107 | + |
| 108 | + var error_text = error.id ? String(error.id) : JSON.stringify(error) |
| 109 | + |
| 110 | + CRITICAL_ERROR( |
| 111 | + I18N("Bad server answer!") + "<br>" |
| 112 | + + I18N("Request URL: ${1}", name) + "<br>" |
| 113 | + + I18N("Text error: ${1}", error_text) + "<br>" |
| 114 | + ); |
| 115 | +}; |
0 commit comments