[英]How to post request using axios on react
how to post request using axiox on reactjs 如何使用上reactjs axiox到POST请求
I am building an simple todo app . 我正在构建一个简单的待办应用程序 。 I use mongodb and nodejs for the backend and reactjs for frontend . 我将mongodb和nodejs用于后端 ,将reactjs用于前端 。 Now i need to know how to post request using axiox on reactjs . 现在我需要知道如何使用上reactjs axiox 张贴请求 。 Because i wanna stored all my data on my server. 因为我想将所有数据存储在服务器上。 I make a crud api for that. 我为此做了一个粗略的 api。
Thanks :) 谢谢 :)
I'm assuming you are mentioning axios instead of axiox 我假设您提到的是axios而不是axiox
Firstly you need to install axios one of the ways is using npm , install its as a dev dependency from terminal/windows 首先,您需要安装axios的方法之一是使用npm,从终端/ Windows将其安装为dev依赖项
npm install --save-dev axios
make sure to add the axios where ever you are making use of axios. 确保将axios添加到您使用axios的位置。
import axios from 'axios';
then you can use whatever request you want use axios for. 然后,您可以使用要使用axios的任何请求。 Below is the code (GET request) from axios npm documentation , 以下是axios npm文档中的代码(GET请求),
axios.get('url')
.then(function (response) {
// You can do whatever you want to do with the response
})
Make sure you add catch for error : 确保您添加了错误捕获:
.catch(function (error) {
console.log(error);
});
You can read more about on axios documentation 您可以在axios文档上阅读有关的更多信息。
If you want to know more about axios go here on Medium 如果您想了解有关axios的更多信息,请点击“ 中型”
This requires a much more detailed answer. 这需要更详细的答案。 Like what happens when you use arrow functions eg .. 就像使用箭头功能(例如..)时发生的情况
axios.get('my/api/names')
.then(function (response) {
// you get back an object with config,data,headers,request,status,statusText
// the api im currently working with hands out an array for the data bit of
//the object then i can loop the data and use it whatever way i want.
response.data.forEach( item => {console.log(item)}
})
Im not 100 percent how this actually works but when your using the key word "then" its just your standard promise, after the then() its good to have a then.catch(err => {console.log(err)}) so it makes debugging that bit easier. 我不是100%这实际上是如何工作的,但是当您使用关键字“ then”只是您的标准承诺时,那么在then()之后使用then.catch(err => {console.log(err)})是件好事。因此它使调试变得容易一些。 The response is passed to the function in the form of an object so you can just use . 响应以对象的形式传递给函数,因此您可以使用。 notation to access the various things in that object. 表示法来访问该对象中的各种事物。 Im struggling a bit myself with axios, ive been reading up on it but there are some weird rules like { data } is required in some cases. 我一直在努力地与axios挣扎,尽管我一直在阅读它,但是在某些情况下还是需要一些奇怪的规则,例如{data}。 Im hoping someone comes up with a better answer. 我希望有人能提出更好的答案。 axios is a really good tool and really helps when you have lots of API requests going off at the same time. 当您同时关闭许多API请求时,axios是一个非常好的工具,并且确实有帮助。 You can use axios.all() to do all requests at the same time 您可以使用axios.all()同时执行所有请求
axios.all(myArray.map(myArr => axios.get('/domain/thing/${myArr.names}/names')))
this bit of code just sends all your requests at the one time which can be handy in some cases. 这段代码仅一次发送所有请求,这在某些情况下很方便。 Keep reading the documentation and trying things out. 继续阅读文档并进行尝试。 I found it useful putting breakpoints in and watching the requests come through in the Network tab in chrome and looking at the way the code is interpreted in the Sources tab and watching the values change line by line. 我发现在chrome的“网络”标签中放置断点并观察请求通过,并在“源”标签中查看代码的解释方式,并观察值逐行更改的情况很有用。 Also a good tool im using in React is MobX which is just a state management tool its pretty interesting and there a good course on eggheads.io which some German guy explains things really well. 在React中使用的一个很好的工具是MobX,它只是一个非常有趣的状态管理工具,并且在eggheads.io上有一个不错的课程,一些德国人对此做得很好。 Best of luck with React 用React祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.