开发者

replace document with AJAX results

开发者 https://www.devze.com 2023-03-01 05:53 出处:网络
I have a form that submits to a script which can take a long time to execute (~30-60 seconds, give or take).I would like to have a spinner that shows the results are processing while the user waits.No

I have a form that submits to a script which can take a long time to execute (~30-60 seconds, give or take). I would like to have a spinner that shows the results are processing while the user waits. Normally, I would stick the spinner in the processing script and be done with it, but I don't have access to the script. Is there a way to use js/jQuery to submit an AJAX request, and then load the target page when it is finished processing? Something like this is what I want:

<script type="text/javascript">
    $(function() {
        $("#myForm").submit(function () {
            var data = $(this).serialize();
            $.post("url/to/script.php", data, function (results) {
                // replace this entire document, url included, with the results
            });
            $("#spinner").hide().ajaxStart(function () {
                $(this).show();
            }).ajaxStop(function () {
                $(this).开发者_Go百科hide();
            });
        });
    });
</script>

I am open to alternative ideas as well.

[edit]

To (hopefully) clarify: I want the results to load in the background, and only navigate to them when they are done. The behavior (with the exception of the spinner) should be no different to the end user than submitting the form without using javascript.

[edit 2]

I managed to convince the owner of the target script to put a spinner in place, so the question is now academic.


If I understand you correctly...you cannot replace the url with javascript. Can you not just window.location.href = "your-new-url.com" on success of the ajax?

If you can't do that you could post your resulting data from the ajax request to the new url and just have that page spit out your result.

0

精彩评论

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