如何从 reactjs 发出 Http 请求? [英] how to Make Http request from reactjs ?

查看:39
本文介绍了如何从 reactjs 发出 Http 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆