-
• 1. axios 发起 post请求 json 传入数据格式
-
• 2. axios get请求
1. axios 发起 post请求 json 传入数据格式
使用 axios
发起 POST 请求并以 JSON 格式传递数据是前端开发中常见的操作。
下面是一个简单的示例,展示如何使用 axios
向服务器发送包含 JSON 数据的 POST 请求。
首先,确保你的项目中已经安装了 axios
。如果还没有安装,可以通过 npm 安装它:
npm install axios
接下来,你可以使用以下代码来发起一个 POST 请求:
import axios from'axios';
// 要发送的数据
const data = {
username: 'example',
password: '123456'
};
// 配置请求选项
const config = {
headers: {
'Content-Type': 'application/json'// 设置请求头为 JSON 格式
}
};
// 发起 POST 请求
axios.post('https://api.example.com/login', data, config)
.then(response => {
co