Replace page with jquery $ .post response

I am using jquery $ .post as follows:

$.post("/searchresult", $("#searchDiv").serialize(), function(data) { //??? });

The "data" parameter has all the html code of the results page that I want.

The question is how to replace the current page content with that returned by the $ .post function.

Thanks,

Rui

+3
source share
5 answers

$('body').html(data); must do it.

+7
source

You can use:

$('body').load("/searchresult", { data : $("#searchDiv").serialize() }, callBackFunction);
+1
source

ajax $('body').empty(), $('body').appendTo(...)

, - innerHtml ( html-), , $('body').html(data) . , IFRAME, src url..

0

, , , .

.ajax POST html, .

, Chrome, async . . , - asycn , , .

- iframe . .

- , json , .

, Chrome, Html. , , , , .

, - .

, $(form).submit() ajax. , html-. , javascript.

, -.

        if (IsChrome()) {
            $.ajax({
                type: 'POST',
                url: oo.url,
                data: oo.data,       // serialized form  $(form).serialize(); althought .serializeArray() also seems to work here.
                dataType: "html",
                cache: false,
                async: true,
                success: function (result, status, xhr) { HandleChromePostAlreadySubmittedSuccess(result, status, xhr); },   // this being the most important one to handle.
                error: function (jqXHR, textStatus, errorThrown) { HandlePostAlreadySubmittedError(jqXHR, textStatus, errorThrown); },
                complete: function (jqXHR, textStatus) { HandlePostAlreadySubmittedComplete(jqXHR, textStatus); }
            });
        } else {
            // will POST(even though load is for GETs). 
            // Chrome does not like the hack way. not sure why, the async status calls stop firing.

        // IMPORTANT use .serializeArray() below - will force a POST instead of a GET.

         // be careful using the jq LIVE on the submit button, may cause multi events to fire.
        // The Complete func below, was not really needed(mines an empty func), unlike the chrome above.
            $.load(oo.url, $(form).serializeArray(), function (responseText, textStatus, XMLHttpRequest) { HandlePostAlreadySubmittedComplete(XMLHttpRequest, textStatus); });
        }



    HandleChromePostAlreadySubmittedSuccess = function (result, status, xhr) {
            // Chrome - this is suppose to be the correct way.
            // Other browsers decided to program write and close a different way, causing the page to unload, from what i understand.
            document.write(result);
            document.close();
    };
0

, , - , . , .

var url = 'action.php';
var form = $('<form action="'+url+'" method="post" />');
$('body').append(form);
form.submit();
0

Source: https://habr.com/ru/post/1725437/


All Articles