如何从 reactjs 发出 Http 请求?
how to Make Http request from reactjs ?
我在 ToDo 应用程序中使用 React js 作为前端,使用 zf3 作为后端。我将所有 React 文件夹和文件放在 Zend 项目的 public 文件夹中。截至目前,它只是简单的应用程序,没有数据库连接。现在我想添加 Db 来存储任务。但是作为一个新手,我不知道如何让Http请求编辑删除和添加任务。请举例说明。任何帮助将不胜感激。谢谢。
http请求有很多npm模块。这是一个微笑的:https://github.com/request/request
我用axios。它允许您设置一些默认配置,这样您就不需要对每个请求都这样做:
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
install axios
$ npm install axios
import axios
import axios from 'axios';
get request
axios.get('api url').then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
post request
var body = {
firstName: 'testName',
lastName: 'testLastName'
};
axios.post('api url',body).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
我在 ToDo 应用程序中使用 React js 作为前端,使用 zf3 作为后端。我将所有 React 文件夹和文件放在 Zend 项目的 public 文件夹中。截至目前,它只是简单的应用程序,没有数据库连接。现在我想添加 Db 来存储任务。但是作为一个新手,我不知道如何让Http请求编辑删除和添加任务。请举例说明。任何帮助将不胜感激。谢谢。
http请求有很多npm模块。这是一个微笑的:https://github.com/request/request
我用axios。它允许您设置一些默认配置,这样您就不需要对每个请求都这样做:
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
install axios
$ npm install axios
import axios
import axios from 'axios';
get request
axios.get('api url').then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
post request
var body = {
firstName: 'testName',
lastName: 'testLastName'
};
axios.post('api url',body).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});