2

I'm trying to fetch from localhost:

export function fetchUsersFromAPI() {
    return(dispatch) => {
        dispatch(getUsers())
         fetch('https://localhost:3000/users')
      // fetch('https://192.168.0.2:3000/users/')  //< same error for this too
        .then(res => res.json())
        .then(json => dispatch(getUsersSuccess(json.results)))
        .catch(err => dispatch(getUsersFailure(err)))
    }
}

Accessing https://192.168.0.2:3000/users or https://localhost:3000/users from browser returns :

[{"id":1,"name":"Name1","color":"Green"},{"id":2,"name":"Name2","color":"Red"},{"id":3,"name":"Name3","color":"Yellow"}]

So its already in json. Is it mandatory to use https and not http in Redux fetch?

I passed the err to my reducer so I get :

"TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:15946:16)
    at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:18719:35)
    at XMLHttpRequest.setReadyState (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:18502:18)
    at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:18358:14)
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:18453:45
    at RCTDeviceEventEmitter.emit (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:9351:35)
    at MessageQueue.__callFunction (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:8235:42)
    at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:8051:15
    at MessageQueue.__guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:8206:9)
    at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=true&minify=false:8050:13)"

What am I doing wrong? If I use fetch('https://swapi.co/api/people/') it works. Please help.

9
  • What is running on 3000? do you have a proxy or is that another server? Commented Sep 14, 2017 at 23:35
  • It's my localhost. Node sever. I used OpenSSL to create the certs. Commented Sep 15, 2017 at 0:05
  • try catching the error with fetch('https://localhost:3000/users').catch(e => console.log(e)) Commented Sep 15, 2017 at 0:12
  • 2
    try using 10.0.2.2:3000 instead of localhost:3000 for android Commented Sep 15, 2017 at 5:29
  • @AnkitAggarwal Same error. Network request failed. Commented Sep 15, 2017 at 6:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.