简体   繁体   中英

Axios can't send a Get Request with header

I'm trying to send a get request to my server. I can post easily but when it comes to Get request it fails.

my code:

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);
            });
        });

the error i get in my debugger :

Error: Request failed with status code 500
    at createError (F:\My_Projects\React_Native_Projects\project\node_modules\axios\lib\core\createError.js:16)
    at settle (F:\My_Projects\React_Native_Projects\project\node_modules\axios\lib\core\settle.js:18)
    at XMLHttpRequest.handleLoad (F:\My_Projects\React_Native_Projects\project\node_modules\axios\lib\adapters\xhr.js:77)
    at XMLHttpRequest.dispatchEvent (F:\My_Projects\React_Native_Projects\project\node_modules\event-target-shim\lib\event-target.js:172)
    at XMLHttpRequest.setReadyState (F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:572)
    at XMLHttpRequest.__didCompleteResponse (F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:394)
    at F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:507
    at RCTDeviceEventEmitter.emit (F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190)
    at MessageQueue.__callFunction (F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:366)
    at F:\My_Projects\React_Native_Projects\project\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:106

what is wrong with the code? I saw examples in forums and it is just like them, any help would be appreciated

我有类似的问题,并通过使用params键修复如下

axios.get(url,{params:{headers:{'my_token'}}}) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM