我有一个golang服务器在端口8080上侦听。我正在尝试从中获取数据(curl确实成功了)但是我的React应用程序在Axios上不断捕获错误,它显示"网络错误!&# 34 ;. 我试过"拒绝授权:假"为了忽略SSL错误,我尝试了#34; 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);
})
}
}
My 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)))
}
答案 0 :(得分:1)
尝试Axios获取没有https的localhost,例如axios.get("http://localhost:8080/data")
。
配置golang https服务器需要使用http.ListenAndServeTLS
,在这种情况下,它由http.ListenAndServe
配置为http。
查看官方文档Accuracy of NSTimer