Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
How do I process two requests to the same address?

How can I process them so that I can use the data of both requests? UI:

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);
}

P.S. Don't beat me with sticks, I'm learning on my own -_-

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To use the same endpointto process two or more different requests you need to provide the means by which the server can choose the appropriate actions to take. As your fetch requests are done using GET you can provide a querystring parameter which is then used server-side to fork program logic. For example:

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

And the modified fetch requests with new querystring parameter

<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>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda