我正在尝试将get请求发送到我的服务器。我可以轻松 post
,但是当涉及Get
请求时,它会失败。
我的代码:
AsyncStorage.getItem('token').then((token) => {
console.log(token);
const config = {
headers: {
// Authorization: token
'Authorization': 'Bearer ' + token
}
};
axios.get('http://10.0.2.2:8000/api/user/devices', config).then((response) => {
console.log(response);
if (response.data.success) {
try {
const devices = realm.objects('user_devices');
realm.write(() => {
realm.delete(devices);
for (let i = 0; i < response.data.data.length; i++) {
realm.create('user_devices', {
id: response.data.data.id,
dev_name: response.data.data.name,
dev_serial: this.state.serialNumber,
sim_number: response.data.data.sim_number
});
}
});
const devs = realm.objects('user_devices');
this.setState({ devices: devs });
} catch (e) {
Alert.alert('error', e);
}
}
})
.catch((e) => {
console.log(e);
});
});
我在调试器中遇到的错误:
Error: Request failed with status code 500
at createError (F:My_ProjectsReact_Native_Projectsprojectnode_modulesaxioslibcorecreateError.js:16)
at settle (F:My_ProjectsReact_Native_Projectsprojectnode_modulesaxioslibcoresettle.js:18)
at XMLHttpRequest.handleLoad (F:My_ProjectsReact_Native_Projectsprojectnode_modulesaxioslibadaptersxhr.js:77)
at XMLHttpRequest.dispatchEvent (F:My_ProjectsReact_Native_Projectsprojectnode_modulesevent-target-shimlibevent-target.js:172)
at XMLHttpRequest.setReadyState (F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:572)
at XMLHttpRequest.__didCompleteResponse (F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:394)
at F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesNetworkXMLHttpRequest.js:507
at RCTDeviceEventEmitter.emit (F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesvendoremitterEventEmitter.js:190)
at MessageQueue.__callFunction (F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:366)
at F:My_ProjectsReact_Native_Projectsprojectnode_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:106
代码有什么问题?我在论坛上看到了示例,就像他们一样,任何帮助都将不胜感激
我也有类似的问题,并通过使用params键如下
修复了axios.get(url,{params:{headers:{'my_token'}}})