0

i am getting the json from api but it is not converting to array previously i got the values but from this code i am not getting the value

this is my code:

componentWillMount() {
    let initialFilename;
    fetch('http://localhost/Generator/FetchfileDetails.php')
        .then(response=>{
            return response.json();
        }).then(data=>{
            alert(data.filename);
        });
}

the key value in my json is filename is to be stored in array how to do???

0

3 Answers 3

1

i recommended u used package axios for GET data,it's so easily to use..

axios.get('API').then(response=>{console.log(response)})

You can insert this code in the componentDidMount function

if u want to see the full documentation axios,This Link

Sign up to request clarification or add additional context in comments.

Comments

1

First thing keep in mind don't use componentWillMount() method to fetch the data.

Ref: https://reactjs.org/docs/faq-ajax.html#how-can-i-make-an-ajax-call

Use following,

componentDidMount(){  
     fetch("http://localhost/Generator/FetchfileDetails.php") 
        .then(res=> res.json())
        .then(data=>{ //here you can set data to state })
}

Comments

0

You can try instead of response.json() you can do:

fetch("http://localhost/Generator/FetchfileDetails.php") .then(response=>{ return JSON.parse(response) }).then(data=>{ alert(data);})

Also, if you say the response is supposed to be an array, you can't use data.filename , you have to do data['filename'] , data.filename is for object responses

1 Comment

[{"slno":"2","filename":"newfile1txt","filepath":"Uploads\/TextFiles\/newfile1txt"},{"slno":"4","filename":"newfile2txt","filepath":"Uploads\/TextFiles\/newfile2txt"}] my json is like this i am not getting the filename i am getting undifined..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.