我正在尝试从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)))
}
}
从浏览器访问https://192.168.0.2:3000/users或https://localhost:3000/users返回:
[{"id":1,"name":"Name1","color":"Green"},{"id":2,"name":"Name2","color":"Red"},{"id":3,"name":"Name3","color":"Yellow"}]
所以它已经在json
。是否必须在Redux fetch中使用https
而不是http
?
我将err
传递给我的减速机,所以我得到了:
"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)"
我做错了什么?如果我使用fetch('https://swapi.co/api/people/')
则可行。请帮忙。