简体   繁体   English

如何从REACT JS获取JSON以在NODE js中实现

[英]How to get JSON from REACT JS to implement in NODE js

I am trying implement REST API using REACT AND NODE. 我正在尝试使用REACT和NODE实现REST API。 How to get JSON from front end(REACT JS)drag and drop images in template ex." https://www.canva.com/templates/ " to store JSON in Mongodb using NODE JS. 如何从前端(REACT JS)获取JSON并将图像拖放到模板ex。“ https://www.canva.com/templates/ ”中,以使用NODE JS将JSON存储在Mongodb中。

Thanks in advance 提前致谢

You can use fetch api to call a particular route and send data along with it to nodejs backend. 您可以使用fetch api调用特定路由,并将数据连同它一起发送到nodejs后端。 You just need to do simply like this: 您只需要这样做:

async function sendData(){
  let res = await fetch(url, {
       method: 'POST',
       mode: 'CORS',
       body: {}, //the json object you want to send
       headers: {}, //if required
     }
  )
}

Hope this helps! 希望这可以帮助!

Since you asked how to send JSON to node.js I'm assuming you do not yet have an API that your front end can use. 因为您问过如何将JSON发送到node.js,所以我假设您还没有前端可以使用的API。

To send data to the back end you need to create an API that accepts data. 要将数据发送到后端,您需要创建一个接受数据的API。 You can do this quickly and easily using express.js . 您可以使用express.js轻松快速地做到这一点。

Once the server is running and it has an endpoint to send data to, you can create a request (eg when sending data it should be a POST request). 服务器运行后,它具有向其发送数据的端点,就可以创建一个请求(例如,在发送数据时,它应该是POST请求)。 This can be done in many different ways, although I would suggest trying axios . 尽管我建议尝试使用axios ,但是可以通过许多不同的方式来完成。

Hope this helped. 希望这会有所帮助。

Check the example to get the Json value and update it. 检查示例以获取Json值并更新它。

axios.get('https://jsonplaceholder.typicode.com/todos/'+ this.props.id + '/')
        .then((res) => {
            this.setState({
                // do some action
            });
        })
        .catch(function (error) {
            console.log(error);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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