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

172
Visualizações
How to get the json data from xhr fetch (after CORS) back in php

I am fetching JSON data with CORS and I am getting the JSON data in my console successfully. Now I want to display the fetched data with PHP. What are the next steps after the code below to get the fetched data into PHP?

    <script>
        fetch("http://localhost:8000/get_users.php", {
            method: 'post',
            credentials: 'include'
        }).then(response => {
            return response.json();
        }).then(users => {
            console.log(users);
        });
    </script>

I tried to get it in $_POST:

    $posted_data = array();
    if (!empty($_POST['users'])) {
        $posted_data = json_decode($_POST['users'], true);
    }
    print_r($posted_data);

But users is undefined.

Does someone know how to get the fetched JSON users into PHP? Thanks!

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

0

Okay I found a solution that might bring me one good step further. I need to JSON.stringify() my fetched JSON, that was the issue.

UPDATED CODE:

<script>
        fetch("http://localhost:8000/get_users.php", {
            method: 'post',
            credentials: 'include'
        }).then(response => {
            return response.json();
        }).then(users => {
            console.log(users); 
            var users = JSON.stringify(users);
            alert(users);
        });
    </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