I am trying to get data from an api but the thing is the api has php extesnion. I am quite new to this thing and I really dont understand why i am not getting the data by using result.data.
var formdata = new FormData();
formdata.append("stock_symbol", "PPL");
var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};
fetch("https://dev-api.sarmaaya.pk/3.0/company_fundamentals.php", requestOptions)
.then(response => response.text())
.then(
result => {
console.log('result', result.data)
}
)
.catch(error => console.log('error', error));
CodePudding user response:
It does not matter from where you are fetching the data you are making a silly mistake, the data format is in json but you are forcibly converting it to raw text so you have to replace result.data
with only result e.g.
var formdata = new FormData();
formdata.append("stock_symbol", "PPL");
var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};
fetch("https://dev-api.sarmaaya.pk/3.0/company_fundamentals.php", requestOptions)
.then(response => response.text())
.then(
result => {
console.log('result', result) //now works
}
)
.catch(error => console.log('error', error));
for request.data
to work you have to replace response.text()
with response.json()
var formdata = new FormData();
formdata.append("stock_symbol", "PPL");
var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};
fetch("https://dev-api.sarmaaya.pk/3.0/company_fundamentals.php", requestOptions)
.then(response => response.json())
.then(
result => {
console.log('result', result.data) //now it works
}
)
.catch(error => console.log('error', error));