Changeset 2051

Show
Ignore:
Timestamp:
06/18/09 16:29:26 (9 months ago)
Author:
JensDiemer
Message:

work-a-round: We must find a way to handle redirects!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/media/PyLucid/pylucid_js_tools.js

    r2037 r2051  
    1313    win = window.open(URL, "", "width=900, height=760, dependent=yes, resizable=yes, scrollbars=yes"); 
    1414    win.focus(); 
     15} 
     16 
     17 
     18function replace_complete_page(html) { 
     19        // replace the complete page 
     20        document.open() // no append available since it is closed 
     21        document.write(html); 
     22        document.close();  
     23} 
     24 
     25function replace_page_content(data, textStatus) { 
     26        /************************************************************************* 
     27        ajax success "handler". 
     28        replace the "#page_content" with the new html response data 
     29        *************************************************************************/ 
     30        log("ajax post response success."); 
     31        log("status:" + textStatus); 
     32        if (data.indexOf("</body>") != -1) { 
     33                // FIXME: We should find a way to handle a  
     34                // redirect directly. But we always get the 
     35                // html data of the redirected page. 
     36                 
     37                log("redirect work-a-round: replace the complete page"); 
     38                log("</body> index:" + data.indexOf("</body>")); 
     39                replace_complete_page(data) 
     40        } else { 
     41                $("#page_content").html(data); 
     42                $("#page_content").animate({opacity: 1}, 500 ); 
     43        } 
     44        load_normal_link = false; 
     45} 
     46 
     47 
     48function ajax_error_handler(XMLHttpRequest, textStatus, errorThrown) { 
     49        /************************************************************************* 
     50        ajax error "handler". 
     51        replace the complete page with the error text (django html traceback page) 
     52        *************************************************************************/ 
     53        log("ajax get response error!"); 
     54        log(XMLHttpRequest); 
     55        var response_text = XMLHttpRequest.responseText; 
     56        log("response_text: '" + response_text + "'"); 
     57        if (!response_text) { 
     58                response_text = "<h1>Ajax response error without any response text.</h1>"; 
     59        } 
     60        replace_complete_page(response_text); 
     61        load_normal_link = true; 
    1562} 
    1663 
     
    4390        log("send form to url:" + url); 
    4491         
    45         var load_normal_link = true; 
     92        load_normal_link = true; 
    4693         
    47         $.ajax({ 
     94        XMLHttpRequest = $.ajax({ 
    4895                async: false, 
    4996            type: "POST", 
     
    5299            dataType: "html", 
    53100             
    54             success: function(form_html){ 
    55                         log("ajax post response success."); 
    56                 $("#page_content").html(form_html); 
    57                 $("#page_content").animate({opacity: 1}, 500 ); 
    58                 load_normal_link = false; 
     101            success: replace_page_content, 
     102            complete: function(XMLHttpRequest, textStatus){ 
     103                // Handle redirects 
     104                log("complete:" + XMLHttpRequest); 
     105                log("text:" + textStatus); 
     106                log("complete:" + XMLHttpRequest.status); 
     107                log("complete:" + XMLHttpRequest.getResponseHeader('Location')); 
     108                 
     109                if(XMLHttpRequest.status.toString()[0]=='3'){ 
     110                        top.location.href = XMLHttpRequest.getResponseHeader('Location'); 
     111                } 
    59112            }, 
    60             error: function(XMLHttpRequest){ 
    61                 log("ajax get response error!"); 
    62                 // Display the complete Traceback html page 
    63                 log(XMLHttpRequest); 
    64                 var response_text = XMLHttpRequest.responseText; 
    65                 log("response_text: '" + response_text + "'"); 
    66                 if (!response_text) { 
    67                         document.write("<h1>ajax response error</h1>"); 
    68                 } else { 
    69                         document.write(response_text); 
    70                 } 
    71                 load_normal_link = true; 
    72             } 
    73         });        
     113            error: ajax_error_handler 
     114        }); 
     115        log("ajax done:" + XMLHttpRequest); 
     116        log("ajax done:" + XMLHttpRequest.status); 
     117        log("ajax done:" + XMLHttpRequest.getResponseHeader('Location')); 
    74118        return load_normal_link; // <-- important: Don't send the form in normal way. 
    75119    });  
     
    104148    log("get:" + url); 
    105149     
    106     var load_normal_link = true; 
     150    load_normal_link = true; 
    107151     
    108152    $.ajax({ 
     
    112156        dataType: "html", 
    113157         
    114         success: function(form_html){ 
    115                 log("ajax get success."); 
    116             $("#page_content").html(form_html); 
    117             $("#page_content").animate({opacity: 1}, 500 ); 
    118             load_normal_link = false; 
    119         }, 
    120         error: function(XMLHttpRequest){ 
    121                 log("ajax get response error!"); 
    122             // Display the complete Traceback html page 
    123                 log(XMLHttpRequest); 
    124                 var response_text = XMLHttpRequest.responseText; 
    125                 log("response_text: '" + response_text + "'"); 
    126                 if (!response_text) { 
    127                         document.write("<h1>ajax response error</h1>"); 
    128                 } else { 
    129                         document.write(response_text); 
    130                 } 
    131                 load_normal_link = true; 
    132         } 
     158        success: replace_page_content, 
     159        error: ajax_error_handler 
    133160    }); 
    134161    if (debug) {