axios.post方法会将参数作为请求体发送给服务器,并不会自动将参数拼接到URL中。
以下是使用axios.post方法发送POST请求的代码示例:
import axios from 'axios';
const data = {
name: 'John',
age: 25
};
axios.post('/api/endpoint', data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的示例中,我们使用axios.post方法发送一个POST请求到/api/endpointURL,并将data作为请求体发送给服务器。服务器会将请求体中的数据作为参数进行处理。
如果你想要将参数拼接到URL中,可以使用axios.get方法发送一个GET请求,示例如下:
import axios from 'axios';
const params = {
name: 'John',
age: 25
};
axios.get('/api/endpoint', { params })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的示例中,我们使用axios.get方法发送一个GET请求到/api/endpointURL,并将params作为参数拼接到URL中。服务器会将URL中的参数进行处理。