使用 fetch 和 axios 发送不记名令牌的麻烦
trouble to sending the bearer token with fetch and axios
我在尝试的两种选择中都遇到了令牌问题
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file, file.name);
try {
const req = await fetch(`${BASE_API}/sales/upload-attachment`, formData, {
method: 'POST',
headers: {
Authorization: token,
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
});
/* console.log('que que ta dando' + JSON.stringify(req)); */
//const json = await req.json();
//return json;
} catch (err) {
console.log(err);
return err.error;
}
},
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem("token");
let params = { sale: id };
let data = new FormData();
data.append("file", file, file.name);
try {
let result = await axios.post(`${BASE_API}/sales/upload-attachment`, {
data,
headers: {
Authorization: `${token}`,
Accept: "application/json",
},
});
console.log(result);
} catch (error) {
console.error(error.response);
}
};
这两个代码无法将令牌传递给 api,这个 api 工作正常,因为网络部分没问题,但在移动设备上我遇到了问题
以这种方式更改格式,
对于fetch
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file);//here should be file object
try {
const req = await fetch(`${BASE_API}/sales/upload-attachment`, {
method: 'POST',
headers: {
Authorization: token,
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body : formData
});
} catch (err) {
console.log(err);
return err.error;
}
}
对于axios
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file);//here should be file object
try {
const req = await axios.post(`${BASE_API}/sales/upload-attachment`, formData, {
headers: {
Authorization: token,
"Content-Type" : "multipart/form-data",
'Accept': 'application/json',
}
});
} catch (err) {
console.log(err);
return err.error;
}
}
我在尝试的两种选择中都遇到了令牌问题
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file, file.name);
try {
const req = await fetch(`${BASE_API}/sales/upload-attachment`, formData, {
method: 'POST',
headers: {
Authorization: token,
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
});
/* console.log('que que ta dando' + JSON.stringify(req)); */
//const json = await req.json();
//return json;
} catch (err) {
console.log(err);
return err.error;
}
},
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem("token");
let params = { sale: id };
let data = new FormData();
data.append("file", file, file.name);
try {
let result = await axios.post(`${BASE_API}/sales/upload-attachment`, {
data,
headers: {
Authorization: `${token}`,
Accept: "application/json",
},
});
console.log(result);
} catch (error) {
console.error(error.response);
}
};
这两个代码无法将令牌传递给 api,这个 api 工作正常,因为网络部分没问题,但在移动设备上我遇到了问题
以这种方式更改格式,
对于fetch
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file);//here should be file object
try {
const req = await fetch(`${BASE_API}/sales/upload-attachment`, {
method: 'POST',
headers: {
Authorization: token,
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body : formData
});
} catch (err) {
console.log(err);
return err.error;
}
}
对于axios
uploadAttachment: async (file, id) => {
const token = await AsyncStorage.getItem('token');
let params = { sale: id };
let formData = new FormData();
formData.append('file', file);//here should be file object
try {
const req = await axios.post(`${BASE_API}/sales/upload-attachment`, formData, {
headers: {
Authorization: token,
"Content-Type" : "multipart/form-data",
'Accept': 'application/json',
}
});
} catch (err) {
console.log(err);
return err.error;
}
}