简体   繁体   中英

React handle form submit after being redirected by server (with data )?

React front-end submits a form (POST) which is sent to the backend. This server then responds with a js object which contains html (I thought it was supposed to redirect)?

How am I supposed to redirect the user to the page that was received?

For example if I curl -X POST api.example.com the api, it returns pure html like this:

<html> ... </html>

When using postman, it automatically renders this html. How am I supposed to deal with this in React?

If you use NodeJS + express

Example:

http.get('*',function(req,res){  
    res.redirect('/'+req.url);
})

https://expressjs.com/en/api.html

You can redirect to specific page using react-router.

Step 1: Wrap your <App /> inside <BrowserRouter />

Go to you component where service is called and then redirect user after receiving service response.

this.route.navigate(['/']);

As you are saying your receiving HTML in response of REST service call. If you want to render this content on page.

let content = null;

content = response.data; // assign data to variable

Now display this in component like this:

class Content extends Component {
  render () {
    return (
     {this.content}
     ); 
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM