【问题标题】:fetch api (GET DATA) from php file从 php 文件中获取 api (GET DATA)
【发布时间】: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 文件中获取变量的值。

【问题讨论】:

标签: javascript php get fetch fetch-api


【解决方案1】:

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

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

【讨论】:

  • 如果我回显该值..那么它肯定会回显.....这是正确的....但我想在 javascript 页面而不是 api 页面上查看 $a 的值。
  • 这不可能,JavaScript 唯一能看到的是 PHP 回显的内容或网络服务器提供的内容。
  • 基本上我正在开发安全应用程序..如果它回显数据..那么它就不安全了..
  • 你能解释一下吗?目前看来您需要某种身份验证?
  • 基本上我现在正在使用 2 台服务器,apache 和 node .js....我的 apache 服务器(php 文件)包含公钥.. 我想从 apache 获取该公钥服务器..这基本上是我想做的..只是解释了一点..在某些网站上它的安全程序相当长。
【解决方案2】:

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

echo 'come!! fetch this value';

【讨论】:

  • 我不想回显它..只是想将它保存在变量中并在 javascript 页面中查看值。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2018-07-22
    • 2011-12-02
    • 2020-12-15
    • 2012-07-02
    相关资源
    最近更新 更多