在react-native中使用Fetch时网络请求失败

时间:2018-05-19 15:33:40

标签: react-native

当我尝试从本地服务器获取API时,我收到以下错误

TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:15823:18)
    at XMLHttpRequest.dispatchEvent (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:17900:39)
    at XMLHttpRequest.setReadyState (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:17655:20)
    at XMLHttpRequest.__didCompleteResponse (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:17482:16)
    at blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:17592:47
    at RCTDeviceEventEmitter.emit (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:3478:37)
    at MessageQueue.__callFunction (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:2384:44)
    at blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:2154:17
    at MessageQueue.__guardSafe (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:2346:11)
    at MessageQueue.callFunctionReturnFlushedQueue (blob:http://localhost:8081/9b6428f5-4e65-45a0-9490-3852c365f0df:2153:14) {stack: TypeError: Network request failed
    at XMLHttpRe…081/9b6428f5-4e65-45a0-9490-3852c365f0df:2153:14),
message: Network request failed}

我从我的客户端访问APi如下

const apiURL = "192.168.1.5:3000/api/";


     fetch(apiURL + '/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: this.props.username,

                    password: this.props.password
                })
            })
                .then((response) => response.json())
                .then((responseJson) => {
                    console.log(responseJson);
                })
                .catch((error) => {
                    console.log(error);
                });

我在我的实际设备上运行应用程序,它运行在android 7.0中。我的笔记本电脑和手机都连接在同一个Wi-Fi上。我检查我的清单文件,是否启用了Internet权限。我不确定我做错了什么。

0 个答案:

没有答案