最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to do post request and render response as a new page? - Stack Overflow

matteradmin6PV0评论

I can send a POST request and render the response as a full page via submitting a form. How can I do that via a ajax request? That is:

$.ajax('test', {
    'method': 'POST',
    'success': function(res) {
        // open a new page, render res as a full page
        // I can't simply do window.location = "foobar" as it's a POST
    }
});

Or, if this is strange, what is the conventional way of doing it?

I can send a POST request and render the response as a full page via submitting a form. How can I do that via a ajax request? That is:

$.ajax('test.', {
    'method': 'POST',
    'success': function(res) {
        // open a new page, render res as a full page
        // I can't simply do window.location = "foobar" as it's a POST
    }
});

Or, if this is strange, what is the conventional way of doing it?

Share Improve this question asked Mar 7, 2015 at 10:44 BoyangBoyang 2,5765 gold badges34 silver badges50 bronze badges 3
  • 2 Why did you choose ajax for this task? – Piotr Łużecki Commented Mar 7, 2015 at 10:47
  • Submit the form using method="POST" instead of using script if the result page is html – mplungjan Commented Mar 7, 2015 at 10:47
  • The whole idea of using Ajax is to not render a plete new page. – GolezTrol Commented Mar 7, 2015 at 10:59
Add a ment  | 

2 Answers 2

Reset to default 3

If you just need to open result of your form processing in new page you don't need ajaxa and you can just use target property of form like

<form action="http://test./" method="post" target="_blank">
Your inputs, etc.
</form>

Well i would go for form if you don't care about response at this point. But if you do, you can check response and do something like this:

$.ajax('test.', {
    'method': 'POST',
    'success': function(res) {
        if ( res.code == 200 )
            document.location = 'page1.htm';
        else
            alert('error!');
    }
});
Post a comment

comment list (0)

  1. No comments so far