English 中文(简体)
replace page with jquery $.post response
原标题:

I m using jquery s $.post like this:

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

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

The question is how do I replace my current page s content with this one returned by the $.post function.

Thanks,

Rui

问题回答

$( body ).html(data); should do it.

You can use:

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

I ve done this with multiple ajax calls to refill a page using $( body ).empty() and then several $( body ).appendTo(...)

Assuming you can arrange for your webservice to return innerHtml of body alone (and not an entire html document), I think just $( body ).html(data) should work. if not, i would consider loading the body with an IFRAME that src s that url.. or similar

Little late for answering, but this looked like the best place to add my solution to s similar problem you were having.

Had a similar situation, need to use the .ajax POST html response, it contained error messages.

Problem that started with Chrome browser, not allowing async posts after a submit. We had a long running process and needed to show status. So our status tracker was making asycn calls while the submit was occurring, worked in most browser, but recently stopped working in chrome.

-One solution would be to use an iframe and call the other server side code that way. Was not an option for me.

-Or change the server side code to return json and handle the stuff that way, lot more work re-writing client and server code already in place.

I Went down the road of trying to fix the async call problem with Chrome while making a submit post and handle the Html response. This is what i came up with, and seems to be working.

Went to so many sites and got so many ideas and knowledge, i can t give direct credit to anyone.

So i basically turned the $(form).submit() into an ajax post. But this still has trouble in chrome and other browsers trying to handle the returned html response. The biggest issue, re-wiring the button events and the javascript.

Hopefully this will help someone.

        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();
    };

What I ve found working is the form being appended to body dynamically and submitted just right after it. This way you utilize traditional post w/o showing the form on the page.

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




相关问题
replace page with jquery $.post response

I m using jquery s $.post like this: $.post("/searchresult", $("#searchDiv").serialize(), function(data) { //??? }); The "data" parameter has all the html code of the result page I want. The ...

(414) Request-URI Too Large when creating pdf in asp.net

I m using the following code (asp.net) to show a pdf for the user: Response.ContentType = "Application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=thePdf.pdf"); //data ...

Downloading pictures/Word documents using ASP.NET

If I put the following code: Response.ContentType = "image/jpeg" Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg") Response.WriteFile(MapPath("capitol.jpg"))...

JSP response content type Excel

when i set response content type is excel. In excel sheet it is dsiplaying 1-Jan instead-of 1-1. here is code snippet <%@ page language="java" contentType="text/html; charset=ISO-8859-1" ...

Response outputstream content length?

I am writing to output stream through various methods. How can I, before I close it, find out content length of the outputstream?

热门标签