如何从reactjs发出Http请求? [英] how to Make Http request from reactjs ?
问题描述
我在ToDo应用程序中将react js用作前端,将zf3用作后端.我把所有的React文件夹和文件都放在Zend项目的公共文件夹中.到目前为止,它只是简单的应用程序,没有数据库连接.现在,我想添加Db来存储任务.但是作为一个新手,我不知道如何发出Http请求以进行编辑删除和添加任务.请举例说明.任何帮助将不胜感激.谢谢.
I am using react js as front end and zf3 as a backend in my ToDo application. I put all my React folder and files in public folder of Zend project. As of now, it is just Simple app there is no database connection. Now I want to add Db for storing tasks. but as a newbie, I don't know how to make Http request for edit delete and add a task. please explain with a example. Any help will be appreciated. Thank you.
推荐答案
我使用 axios .它允许您设置一些默认配置,这样您就不必对每个请求都这样做:
I use axios. It allows you to set some default configuration so that you don't need to do it with every request:
axios.defaults.headers.common.Authorization = "my-awesome-token";
axios.defaults.baseURL = http://www.somehost.com/api;
...
axios.get('/people')
.then(response => handleResponse(response))
.catch(error => handleError(error))
// actually shoots to http://www.somehost.com/api/people with Authorization header
这篇关于如何从reactjs发出Http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!