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

标签 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 文件中获取变量的值吗?

最佳答案

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

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

关于javascript - 从 php 文件中获取 api (GET DATA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39414008/

相关文章:

javascript - 从客户端向服务器发送伪造的 socket.io 请求(node.js/socket.io)

php - 更新数据按钮

php - 如果字段不为空,如何显示带有标题的表格一次,后跟多个数据库字段内容?

php - 保护 PHP 被其他站点访问

python - 将文件返回给 WSGI GET 请求

c# - 可以使用 API GET 但不能使用 API POST

javascript - Express 似乎忽略了根路由的 Controller

javascript - 是否可以通过 jquery 将值传递到 <div data-value =""> ?

javascript - HTML跳转页面(使该部分跳转到页面顶部)

php - 如何将表单数据添加到 JSON 文件?