从 php 文件中获取 api(获取数据)

Man*_*roi 5 javascript php get fetch-api

我正在尝试从 php 文件中获取值

api.php

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

      ?>
Run Code Online (Sandbox Code Playgroud)

我的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);
        })
Run Code Online (Sandbox Code Playgroud)

你能指导我如何使用 fetch 从 php 文件中获取变量的值吗?

Mar*_*HoH 4

您没有回显该值,因此不会发送该值。

<?php
// want to fetch this value 
$a = 'come!! fetch this value';
echo $a;
?>
Run Code Online (Sandbox Code Playgroud)