admin管理员组

文章数量:1435859

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!');
    }
});

本文标签: javascriptHow to do post request and render response as a new pageStack Overflow