要将JSON数据或表单数据发送到REST终点,请按照以下步骤进行:
1.在代码中引入Axios包:
import axios from 'axios';
2.使用以下语法来发送JSON数据:
const postData = { name: 'John', surname: 'Doe' }; axios.post('https://example.com/api', postData, { headers: { 'Content-Type': 'application/json' } }).then(response => { console.log(response); }).catch(error => { console.log(error); });
3.使用以下语法来发送表单数据:
const postData = new FormData(); postData.append('name', 'John'); postData.append('surname', 'Doe'); axios.post('https://example.com/api', postData).then(response => { console.log(response); }).catch(error => { console.log(error); });
这样,您就可以将JSON数据或表单数据成功发送到REST终点。