includes/clientside/static/ajax.js
changeset 259 112debff64bd
parent 241 39c132e69781
child 335 59fee40b4644
equal deleted inserted replaced
258:8be996c3740d 259:112debff64bd
    38   {
    38   {
    39     ajax.setRequestHeader("Content-length", parms.length);
    39     ajax.setRequestHeader("Content-length", parms.length);
    40   }
    40   }
    41   ajax.setRequestHeader("Connection", "close");
    41   ajax.setRequestHeader("Connection", "close");
    42   ajax.send(parms);
    42   ajax.send(parms);
       
    43 }
       
    44 
       
    45 /**
       
    46  * Show a friendly error message depicting an AJAX response that is not valid JSON
       
    47  * @param string Response text
       
    48  * @param string Custom error message. If omitted, the default will be shown.
       
    49  */
       
    50 
       
    51 function handle_invalid_json(response, customerror)
       
    52 {
       
    53   var mainwin = $('ajaxEditContainer').object;
       
    54   mainwin.innerHTML = '';
       
    55   
       
    56   // Title
       
    57   var h3 = document.createElement('h3');
       
    58   h3.appendChild(document.createTextNode('The site encountered an error while processing your request.'));
       
    59   mainwin.appendChild(h3);
       
    60   
       
    61   if ( typeof(customerror) == 'string' )
       
    62   {
       
    63     var el = document.createElement('p');
       
    64     el.appendChild(document.createTextNode(customerror));
       
    65     mainwin.appendChild(el);
       
    66   }
       
    67   else
       
    68   {
       
    69     customerror  = 'We unexpectedly received the following response from the server. The response should have been in the JSON ';
       
    70     customerror += 'serialization format, but the response wasn\'t composed only of the JSON response. There are three possible triggers';
       
    71     customerror += 'for this problem:';
       
    72     var el = document.createElement('p');
       
    73     el.appendChild(document.createTextNode(customerror));
       
    74     mainwin.appendChild(el);
       
    75     var ul = document.createElement('ul');
       
    76     var li1 = document.createElement('li');
       
    77     var li2 = document.createElement('li');
       
    78     var li3 = document.createElement('li');
       
    79     li1.appendChild(document.createTextNode('The server sent back a bad HTTP response code and thus sent an error page instead of running Enano. This indicates a possible problem with your server, and is not likely to be a bug with Enano.'));
       
    80     var osc_exception = ( window.location.hostname == 'demo.opensourcecms.com' ) ? ' This is KNOWN to be the case with the OpenSourceCMS.com demo version of Enano.' : '';
       
    81     li2.appendChild(document.createTextNode('The server sent back the expected JSON response, but also injected some code into the response that should not be there. Typically this consists of advertisement code. In this case, the administrator of this site will have to contact their web host to have advertisements disabled.' + osc_exception));
       
    82     li3.appendChild(document.createTextNode('It\'s possible that Enano triggered a PHP error or warning. In this case, you may be looking at a bug in Enano.'));
       
    83       
       
    84     ul.appendChild(li1);
       
    85     ul.appendChild(li2);
       
    86     ul.appendChild(li3);
       
    87     mainwin.appendChild(ul);
       
    88   }
       
    89   
       
    90   var p2 = document.createElement('p');
       
    91   p2.appendChild(document.createTextNode('The response received from the server is as follows:'));
       
    92   mainwin.appendChild(p2);
       
    93   
       
    94   var pre = document.createElement('pre');
       
    95   pre.appendChild(document.createTextNode(response));
       
    96   mainwin.appendChild(pre);
       
    97   
       
    98   var p3 = document.createElement('p');
       
    99   p3.appendChild(document.createTextNode('You may also choose to view the response as HTML. '));
       
   100   var a = document.createElement('a');
       
   101   a.appendChild(document.createTextNode('View as HTML...'));
       
   102   a._resp = response;
       
   103   a.id = 'invalidjson_link';
       
   104   a.onclick = function()
       
   105   {
       
   106     var mb = new messagebox(MB_YESNO | MB_ICONEXCLAMATION, 'Do you really want to view this response as HTML?', 'If the response was changed during transmission to include malicious code, you may be allowing that malicious code to run by viewing the response as HTML. Only do this if you have reviewed the response text and have found no suspicious code in it.');
       
   107     mb.onclick['Yes'] = function()
       
   108     {
       
   109       var html = $('invalidjson_link').object._resp;
       
   110       var win = window.open('about:blank', 'invalidjson_htmlwin', 'width=550,height=400,status=no,toolbars=no,toolbar=no,address=no,scroll=yes');
       
   111       win.document.write(html);
       
   112     }
       
   113     return false;
       
   114   }
       
   115   a.href = '#';
       
   116   p3.appendChild(a);
       
   117   mainwin.appendChild(p3);
    43 }
   118 }
    44 
   119 
    45 function ajaxEscape(text)
   120 function ajaxEscape(text)
    46 {
   121 {
    47   /*
   122   /*
   924         unsetAjaxLoading();
   999         unsetAjaxLoading();
   925         var resptext = String(ajax.responseText + ' ');
  1000         var resptext = String(ajax.responseText + ' ');
   926         resptext = resptext.substr(0, resptext.length-1);
  1001         resptext = resptext.substr(0, resptext.length-1);
   927         if ( resptext.substr(0, 1) != '{' )
  1002         if ( resptext.substr(0, 1) != '{' )
   928         {
  1003         {
   929           alert('Invalid JSON response from server:\n' + resptext);
  1004           handle_invalid_json(resptext);
   930           return false;
  1005           return false;
   931         }
  1006         }
   932         var json = parseJSON(resptext);
  1007         var json = parseJSON(resptext);
   933         var catbox = document.getElementById('mdgCatBox');
  1008         var catbox = document.getElementById('mdgCatBox');
   934         if ( !catbox )
  1009         if ( !catbox )
  1024         addtag_nukeme = false;
  1099         addtag_nukeme = false;
  1025         var resptext = String(ajax.responseText + ' ');
  1100         var resptext = String(ajax.responseText + ' ');
  1026         resptext = resptext.substr(0, resptext.length-1);
  1101         resptext = resptext.substr(0, resptext.length-1);
  1027         if ( resptext.substr(0, 1) != '{' )
  1102         if ( resptext.substr(0, 1) != '{' )
  1028         {
  1103         {
  1029           alert('Invalid JSON response from server:\n' + resptext);
  1104           handle_invalid_json(resptext);
  1030           return false;
  1105           return false;
  1031         }
  1106         }
  1032         var json = parseJSON(resptext);
  1107         var json = parseJSON(resptext);
  1033         var parent = nukeme.parentNode;
  1108         var parent = nukeme.parentNode;
  1034         parent.removeChild(nukeme);
  1109         parent.removeChild(nukeme);