我在申请中无法通过授权。 我的jwt令牌
export const update_profile = ({
about,
birthday,
birth_location,
residence_location,
occupation,
company_name,
gender,
marital_status,
phone
}) => {
return dispatch => {
dispatch(
{
type: UPDATE_PROFILE
}
)
const token = AsyncStorage.getItem('@token_jwt')
let url = "/update_profile"
Axios.post(`${SERVER}${url}`, {
headers: {
"Authorization": `Bearer ${token}`
},
"user": {
"about": about,
"birthday": birthday,
"birth_location": birth_location,
"residence_location": residence_location,
"occupation": occupation,
"company_name": company_name,
"gender": gender,
"marital_status": marital_status,
"cellphone": phone
}
})
.then((response) => {
updateProfileSuccess(dispatch, response)
})
.catch((err) => {
updateProfileError(err, dispatch)
})
}
}
这是我的代码,我总是有未经授权的退货,我的令牌在用户登录时保存在AsyncStorage中。
有人可以帮我吗
答案 0 :(得分:1)
对于Axios,标头需要作为第三个参数(而不是第二个)提供。
赞
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
const data = {
"user": {
"about": about,
"birthday": birthday,
"birth_location": birth_location,
"residence_location": residence_location,
"occupation": occupation,
"company_name": company_name,
"gender": gender,
"marital_status": marital_status,
"cellphone": phone
}
}
axios.post(`${SERVER}${url}`, data, {
headers: headers
})
.then((response) => {
...
})
.catch((error) => {
...
})
或者,您可以传递一个参数,该参数应该是一个对象
...
const options = {
method: 'POST',
headers: headers,
data: data,
url: `${SERVER}${url}`,
};
axios(options);