Manpreet Oberoi Manpreet Oberoi - 2 months ago 23
Javascript Question

fetch api (GET DATA) from php file

I am trying to fetch values from php file

api.php

<?php
// want to fetch this value
$a = 'come!! fetch this value';

?>


my javascript page has code like this.(this code is not on page api.php)

fetch('http://localhost/react_task/react-webpack-boilerplate/php/api.php', {
method: 'get',
// may be some code of fetching comes here
}).then(function(response) {
if (response.status >= 200 && response.status < 300) {
return response.text()
}
throw new Error(response.statusText)
})
.then(function(response) {
console.log(response);
})


can you please guide me how to fetch value of variable from php file by using fetch.

Answer

You are not echoing the value, so it won't be send.

<?php
// want to fetch this value 
$a = 'come!! fetch this value';
echo $a;
?>
Comments