0

I have a respone.send in nodejs application:

this.app.post('/profile', (req, res) => {                        
            let password = req.body.password;            
            let newWallet = operator.createWalletFromPassword(password);
            let projectedWallet = projectWallet(newWallet);
            return res.send(projectedWallet);          
});

And every time I press button in html, it always do my post and return JSON result in html page.

{"id":"f4c02f2463c8f83218f324d4193e08b8812fe914e24063217d29e65966abbe85","addresses":[]}

But I don't want to see it, so how can I hidden or redirect to another page (like '/profile' that I set before)?

EDIT: This is my client side code

<form action="/profile" method="post">
                        <p>
                            <% var ten = user.username %>
                            <strong>id</strong>: <%= user.id %><br>
                            <strong>username</strong>: <%= ten %><br>
                            <strong>password</strong>: <%= user.password %>
                        </p>
                        <p id="passwd" name="passwd" ><%= user.username %></p>
                        <textarea id="myTextArea" cols=50 rows=10></textarea>
                        <!-- these fields will be sent to server -->
                        <input type="hidden" name="username" value="<%= user.username %>">
                        <input type="hidden" name="password" value="<%= user.password %>">

                        <button type="submit" class="btn btn-warning btn-lg" onclick="/profile">Wallet</button>
                    </form>
Meowth2018
  • 21
  • 1
  • 6

1 Answers1

0

The problem is that you are doing a POST from the browser to node. The results of this POST will be displayed by the browser.

If you don't want to see the JSON data then change:

return res.send(projectedWallet);          

To something like:

res.redirect('new url');

or:

res.render('some page');

or:

res.send('I hate json! ;)');

If you really do want the JSON the I suggest changing from:

return res.send(projectedWallet);

to:

res.json(projectedWallet);

since that sets all the proper headers and properly stringifies the JSON output.

UPDATE

If you want to create a form that will post your values and then place the JSON into a text area, as you describe in your comment to this answer then you need to use client-side java script and an AJAX call.

See https://stackoverflow.com/a/46642899/1253074 for further details.

Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • Thank you. But like you said now it not show in my page. But do have some way to put the json result into textarea in client side code? – Meowth2018 Jun 06 '18 at 01:45