【发布时间】:2018-03-23 22:54:35
【问题描述】:
我有一个监听 8080 端口的 golang 服务器。我正在尝试从中获取数据(curl 确实成功获取了它)但是我的 React 应用程序不断在 Axios Get 上捕获错误,它显示“网络错误!”。 我尝试“rejectUnauthorized: false”来忽略 SSL 错误,我在服务器上尝试了“AllowedOrigins: []string{”http://localhost:3000"}”以允许来自我的 React 应用程序的请求。 我仍然收到错误,Firefox 网络选项卡显示:“ssl_error_rx_record_too_long”。
我的 React 代码如下所示:
//Action creators
export const fetchDataRequest = () => ({
type: FETCH_REQUEST
});
export const fetchDataSuccess = (elements) => ({
type: FETCH_SUCCESS,
payload: {elements}
});
export const fetchDataError = (error) => ({
type: FETCH_ERROR,
payload: {error}
});
export function fetchDataWithRedux() {
return function (dispatch) {
dispatch(fetchDataRequest());
axios.get("https://localhost:8080/data")
.then((response) =>{
dispatch(fetchDataSuccess(response.json));
console.log(response);
})
.catch (error => {
dispatch(fetchDataError(error))
console.log(error.message);
})
}
}
我的 Main.go 看起来像这样:
func main() {
//Call the NewRouter function defined in route pkg
route := n.NewRouter()
//convert h.handlers to a http.HandlerFunc
JSONHandler := http.HandlerFunc(h.JsonHandler)
DATAHandler := http.HandlerFunc(h.NixDataHandler)
//Give the handle to the created router
http.Handle("/", route)
route.Handle("/nix", h.JsonHandlerWrapper(JSONHandler))
route.Handle("/data", h.JsonHandlerWrapper(DATAHandler))
//Call function to fetch data from nix.org periodically
timer := time.NewTimer(time.Second)
go func() {
<- timer.C
//call function
data.GetNixData()
}()
//Handling CORS requests
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"},
AllowedMethods: []string{"GET", "POST"},
})
//Start listening on port 8080
log.Fatal(http.ListenAndServe(":8080", c.Handler(route)))
}
【问题讨论】: