I wrote Action, should get data from db.json file. But
Error: message "xhr.js:178 GET http://localhost:8083/data/db.json 404 (Not Found)".
Why is it, if my path is correct (db.json is in the same folder)? In profileActions.js:
import axios from "axios";
var customData = require('./db.json');
export function fetchUsers(){
return function(dispatch){
axios.get('./db.json')
.then((response) => {
dispatch({type:'FETCH_USERS_FULFILLED', payload:response.data});
})
.catch((err) => {
dispatch({type:'FETCH_USERS_REJECTED', payload:err});
})
}
}
Axios cannot read from the file system. It can only make HTTP calls. If you are running in a node environment, look into the fs package, this will let you read from the local file system. Otherwise you will need to expose the json file via a webserver to make it accessible to Axios.
A GET request can be made with Axios to “get” data from a server. The HTTP get request is performed by calling axios. get() . The get() method requires two parameters to be supplied to it.
Axios automatically transforms the data returned from the server, but with fetch() you have to call the response. json method to parse the data to a JavaScript object.
I meet the similar problem before.I create my react app by create-react-app command.And find that you can only get the static file from the public folder.If you want to the get "db.json",you should put the "db.json" into the public folder.
I know that this question is answered long back. It will help people like me. Instead of using axios or other network libraries you can directly import the json file like below and return it in dispatcher. If you are using local json to test it why do you need network libraries.
import data from './response.json'
export function initializeApp() {
return function (dispatch) {
dispatch({
type: ActionTypes.ActionName,
payload: data
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With