我已经成功运行:How to modify axios instance after exported it in ReactJS?
它看起来像这样:
import axios from 'axios';
import constants from '../constants.js';
import Cookies from 'js-cookie';
const API = axios.create({
baseURL: `${constants.urlBackend}`,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
API.interceptors.request.use(
config => {
var accesstoken = Cookies.get('accesstoken');
if (accesstoken) {
config.headers.Authorization = `Bearer ${accesstoken}`;
} else {
delete API.defaults.headers.common.Authorization;
}
return config;
},
error => Promise.reject(error)
);
export default API;
这是一个示例用法
getUserList() {
API.get('/userlist')
.then(response => {
this.setState({
userList: response.data
}, () => {
console.log(this.state.userList)
});
})
}
但现在我很困惑,因为我不明白如何在帖子中使用它,以便我可以向它传递一些数据,类似于此
axios({
method: 'post',
url: constants.urlBackend + "/register",
data: qs.stringify({ email, password }),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
})
但是使用上面的对象。
API.post('/user/update/'+this.state.rowId).then(response => {
//some sort of body {email,password}
})
最佳答案
你试过吗
API.post(
'/user/update/' + this.state.rowId, {
email,
password
}).then(response => {})
关于javascript - 如何使用允许 GET 的承载 token 来调整这个 axios 对象,以使用 POST 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60503697/