Axios无法发送带有标头的Get请求

时间:2019-02-24 10:02:44

标签: javascript reactjs react-native axios

我正在尝试将获取请求发送到我的服务器。我可以轻松地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_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

代码有什么问题?我在论坛上看到了示例,就像它们一样,任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我有类似的问题,并通过使用以下params键进行了修复

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