我想用axios
替换fetch,但无法使其正常工作。任何人都可以帮忙吗?
我有以下代码:
fetch(API_ENDPOINT + "signup", {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data'
},
body: data
})
.then(response => response.json())
.then(response => {
console.log(response);
dispatch({ type: SIGNUP_SUCCESS });
dispatch({ type: LOGIN_SUCCESS, payload: response });
navigator.resetTo({
screen: 'carapp.Phone',
animated: true
});
}).catch(error => console.log(error));
答案 0 :(得分:1)
您应该在此处查看axios文档:https://github.com/axios/axios 并参考"执行POST请求",您可以看到此示例:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
/ user 是 API_ENDPOINT +"注册" 和 { firstName:' Fred', lastName:' Flintstone' } 是你的身体。因此,要使其发挥作用,您需要做的是:
axios.post(API_ENDPOINT + "signup", body)
.then(res => res.json())
.then(response => {
console.log(response);
dispatch({ type: SIGNUP_SUCCESS });
dispatch({ type: LOGIN_SUCCESS, payload: response });
navigator.resetTo({
screen: 'carapp.Phone',
animated: true
});
})
当然,请确保导入并安装axios。