简体   繁体   English

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

[英]fetch api (GET DATA) from php file

I am trying to fetch values from php file我正在尝试从 php 文件中获取值

api.php 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)我的 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);
        })

can you please guide me how to fetch value of variable from php file by using fetch.你能指导我如何使用 fetch 从 php 文件中获取变量的值吗?

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;
?>

Your php needs to output the value you want.您的 php 需要输出您想要的值。 A simple way to do this is to use echo .一个简单的方法是使用echo For example:例如:

echo 'come!! fetch this value';

您应该使用 echo 在 php 中显示它,然后它将在您的 JavaScript 响应中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM