action.js
import Axios from 'axios';
import { PRODUCT_LIST_FAIL, PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS } from '../constraints/productConstraints';
const listProducts = () => async (dispatch) => {
try {
dispatch({ type: PRODUCT_LIST_REQUEST });
const { data } = await Axios.get('http://localhost:3001/product');
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
}
catch(error) {
dispatch({ type: PRODUCT_LIST_FAIL, payload: error.message });
}
}
export { listProducts }home.js
const Home = () => {
const productList = useSelector(state => state.productList);
const { products, loading, error } = productList;
const dispatch = useDispatch();
useEffect(() => {
dispatch(listProducts());
return () => {
}
},[]);
}当我使用axios.get('/product')时,它可以工作,但是当我使用axios.get(‘http://localhost:3001/product'’)时,它显示错误GET http://localhost:3001/product net::ERR_CONNECTION_REFUSED不要为什么我得到这个错误API is not命中
发布于 2020-09-25 01:38:08
如果你还没有这样做,在你的React应用中添加"proxy": "http://localhost:3001",到你的package.json中。
https://stackoverflow.com/questions/64051506
复制相似问题