从php文件中获取api(GET DATA)

时间:2016-09-09 14:33:16

标签: javascript php get fetch fetch-api

我正在尝试从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文件中获取变量值。

3 个答案:

答案 0 :(得分:3)

你没有回应价值,所以它不会被发送。

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

答案 1 :(得分:0)

你的php需要输出你想要的值。一种简单的方法是使用echo。例如:

echo 'come!! fetch this value';

答案 2 :(得分:0)

嘿,尝试将Dom从HTML页面获取,并使用innerHTML从php脚本中获取响应