【发布时间】:2017-01-17 17:57:29
【问题描述】:
我正在尝试从 php 文件中获取值
api.php
<?php
// want to fetch this value
$a = 'come!! fetch this value';
?>
我的 javascript 页面有这样的代码。(此代码不在页面 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);
})
请指导我如何使用 fetch 从 php 文件中获取变量的值。
【问题讨论】:
-
简单。在您的 PHP 页面中,将
echo $a;放在末尾。然后当你抓取页面时,它会抓取页面的文本内容,也就是你回显的内容。 -
没有重复的人.. @DavidR
标签: javascript php get fetch fetch-api