开发者

Replace current page with ajax content

I have a page with a dialog window which sends ajax post data to server and receives a response. During development, there can be two responses - one regular (this is not the question) or one with an error. Server returns code 500 and a page with lot of debug informations. This is a regular page returned from a framework and contains some javascript code. I want to be able to display this error page in case it ha开发者_如何学Pythonppens.

The problem is, I can not simply attach the returned result to body element or open a new link in a new page and load this error again. I simply get a html page instead of data and I have to display the page (in current window or in another one).

I am using jQuery.


Configure jQuery ajax setup as follows:

$.ajaxSetup({
    error: handleXhrError
});

where handleXhrError function look like this:

function handleXhrError(xhr) {
    document.open();
    document.write(xhr.responseText);
    document.close();
}

See also:

  • Handling of server-side HTTP 4nn/5nn errors in jQuery


You may also try to use data URL's, the latest versions of the major browsers supporting it:

function utf8_to_b64( str ) {
    return window.btoa(unescape(encodeURIComponent( str )));
}

function loadHtml(html)
{
    localtion.href='data:text/html;base64,'+utf8_to_b64(html);
}

This way, you can load any html page you want in runtime.


In your ajax callback:

success: function (data) {
   $("html").html($(data).find("html").html());
}

That will replace the entire page's HTML content with the one received from your AJAX request. Works in Chrome... not sure about IE.

Despite that, I'm not sure why you'd want to include the <head> section... but you can easily modify the above to display just what's in the body of the AJAX response, and append it to a div or even a lightbox. Much nicer.


Here is an example of how to change either if the response is a url or a html content (using django\php)

        var xmlhttp;
        xmlhttp=new XMLHttpRequest();

        xmlhttp.onreadystatechange=function()
        {
            var replace_t = '{{ params.replace_t }}';
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                if(replace_t == 'location')
                    window.location.replace(xmlhttp.responseText);
                else if(replace_t == 'content')
                {
                    document.open();
                    document.write(xmlhttp.responseText);
                    document.close();
                } 
            }
        }

        xmlhttp.open("GET",SOME_ASYNC_HANDLER_URL,true);
        xmlhttp.send();


I found this solution. I don't know if it si correct, but for Opera and Firefox it is working.

var error_win = window.open(
   '',
   'Server error',
   'status=0,scrollbars=1, location=0'
);
error_win.document.write(XMLHttpRequest.responseText);


Have you tried just simply creating an element and inserting the returned error page into the element? I do this with error pages and jQuery.

    var errorContainer = $( '<div/>' );
    errorContainer.html( errorTextResponse );
    errorContainer.appendTo( $( 'body' ) );


I may be misunderstanding, but do you know what elements from the result you specifically want to display? You could trying something like this:

  success: function(data){
               //store the response
               var $response=$(data);
               //use .find() to locate the div or whatever else you need
               var errorMessage = $response.find('#warning').text();
               alert(errorMessage);      
            }

Is that what you were looking for?


I don't think there's any way to do that. Iframes are meant for loading other pages and there's no other sandbox in which to dump a standalone page -- that's what frames were designed for.

It might be difficult with the framework you're using, but it's probably worthwhile to have it generate different errors for your Ajax requests. My Ajax pages will only ever send

{"exit": 1, "message": "As in the shell, a non-zero exit is an error and this is why..."}


Just figured this out as easy as

document.body.innerHTML = YourAjaxrequest.responseText;

_______________________________________________^ up here is what over writes your current HTML page with the response.

request.onreadystatechange = function() {
      if (request.readyState == 1) {
        document.getElementById('sus').innerHTML = "SENDING.......";
    } 
      if (request.readyState == 3){
        document.getElementById('sus').innerHTML = "SENDING >>>>>>>>>>>>>";

    }

     if (request.readyState == 4 && request.status == 200) {

        //document.getElementById('sus').innerHTML = request.responseText;

    document.body.innerHTML = request.responseText;


     }
  }
   request.send(formD);



  },false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜