Replace current page with ajax content

前端 未结 9 1256
南旧
南旧 2020-12-05 04:50

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 th

相关标签:
9条回答
  • 2020-12-05 05:05

    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..."}
    
    0 讨论(0)
  • 2020-12-05 05:14

    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' ) );
    
    0 讨论(0)
  • 2020-12-05 05:15

    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();
    
    0 讨论(0)
  • 2020-12-05 05:16

    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?

    0 讨论(0)
  • 2020-12-05 05:23

    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.

    0 讨论(0)
  • 2020-12-05 05:28

    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);
    
    0 讨论(0)
提交回复
热议问题