【问题标题】:How do I process two requests to the same address?如何处理对同一地址的两个请求?
【发布时间】:2022-01-06 23:33:27
【问题描述】:

如何处理它们以便我可以使用这两个请求的数据? 用户界面:

async function getSpecialties(){
  let res = await fetch ('http://server-npk-web-core/specialties');
  let specialties = await res.json();
  })
}
async function getSubjectsSpecial(){
  let res = await fetch ('http://server-npk-web-core/specialties');
  let subjectsSpecial = await res.json();
  })
}

BLL: index.php

if($method === 'GET'){
  if($type === 'subjects'){
    getSubjects($pdo);
  } elseif($type === 'specialties'){
    getSpecialties($pdo);
    getSubjectsSpecial($pdo);
  }

specialties.php

function getSpecialties($pdo){
  $specialties = 'SELECT * FROM `specialties`';
  $stmt = $pdo -> query($specialties);
  while ($special = $stmt->fetch()){
    $specialtiesList[] = $special;
  }
  echo json_encode($specialtiesList);
}
function getSubjectsSpecial($pdo){
  $subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
  $stmt = $pdo -> query($subjectsSpecial);
  while ($subjectSpecial = $stmt->fetch()){
    $subjectsSpecialList[] = $subjectSpecial;
  }
  echo json_encode($subjectsSpecialList);
}

附:别拿棍子打我,我是自学的-_-

【问题讨论】:

  • 创建另一个异步函数并等待提供的两个函数?
  • 两个请求当前看起来相同。为每个请求使用查询字符串参数,以便后端脚本可以调用相应的函数并发送适当的响应

标签: javascript php api


【解决方案1】:

要使用相同的endpoint 处理两个或多个不同的请求,您需要提供服务器可以选择要采取的适当操作的方法。由于您的fetch 请求是使用GET 完成的,因此您可以提供一个查询字符串参数,然后在服务器端使用该参数来分叉程序逻辑。例如:

<?php

    //-------------------------------
    //server-npk-web-core/specialties
    //-------------------------------
    
    
    function getSpecialties($pdo){
      $specialties = 'SELECT * FROM `specialties`';
      $stmt = $pdo -> query($specialties);
      while ($special = $stmt->fetch()){
        $specialtiesList[] = $special;
      }
      echo json_encode($specialtiesList);
    }
    
    function getSubjectsSpecial($pdo){
      $subjectsSpecial = 'SELECT `subjects`.`title` FROM `subjects` WHERE `id_specialties` = 2';
      $stmt = $pdo -> query($subjectsSpecial);
      while ($subjectSpecial = $stmt->fetch()){
        $subjectsSpecialList[] = $subjectSpecial;
      }
      echo json_encode($subjectsSpecialList);
    }
    
    
    
    
    if( !empty( $_GET['action'] ) ){
    
        switch( $_GET['action'] ){
            case 'specialties':
                getSpecialties($pdo)
            break;
            case 'subjectsspecial':
                getSubjectsSpecial( $pdo );
            break;
            default:
                echo 'Error';
            break;
        }
        
    }
?>

修改后的fetch 请求带有新的查询字符串参数

<script>
    async function getSpecialties(){
      let res = await fetch ('http://server-npk-web-core/specialties?action=specialties');
      let specialties = await res.json();
      })
    };
    async function getSubjectsSpecial(){
      let res = await fetch ('http://server-npk-web-core/specialties?action=subjectsspecial');
      let subjectsSpecial = await res.json();
      })
    };
</script>

【讨论】:

  • 我按照你的建议做了,但是这样弹出很多错误disk.yandex.ru/i/Dh2kMOIq0BRJpQ
  • 您提供的链接不起作用 - 或者如果它起作用,我无法理解所说的内容
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多