I want to print the data in console fetched from api
My php file code is<?php echo "Hello"; ?>
My javascript file code is
function cities(str){
alert(str);
let url = "cities.php";
fetch(url).then( (response)=>{return response; }).then((data)=>{console.log(data);});
}
I am calling this function using onclick attribute from HTML file and I want to see Hello
on my console but it giving me output as like this and when I change my javascript file code like
function cities(str){
alert(str);
let url = "cities.php";
fetch(url).then( (response)=>{return response.text(); }).then((data)=>{console.log(data);});
}
then in console it giving output as whole code of php file see here
Your second code snippet (with response.text()
) is correct from the JavaScript side.
You are getting the whole PHP file now because your webserver does not execute the file and return the evaluation result but treats it as a normal file.
What you need to do depends now on the webserver (eg apache2
, nginx
) and the way you want your PHP to be executed (eg CGI
, fpm
). You could look for Configure <your webserver> for PHP
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.