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

158
Visualizações
Get JSON with PHP from URL and hand it over to JS

i'm stuck at a problem. What i wanna do is to get a JSON file from a URL by using PHP (to overcome CORS issue when using AJAX) and then using JSON.parse to make it usable by JavaScript to make a list out of the JSON content.

I tried this code before but it isn't working.

<!DOCTYPE html>
<html>
<head>
    <title>Beispiel</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <?php
        $url = "http://geoweb.zamg.ac.at/static/event/lastday.json";
        ?>

        <script>
            var array = JSON.parse(<?php file_get_contents($url) ?>);
            console.log(array);
        </script>
</body>
</html>

Maybe someone of you can help me with that problem. I'm not really good at programming yet ;)

Many thanks in advance!

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

0

You don't need to JSON.parse(), because the JSON is already parsed from the file source.

You also missed to echo the return of file_get_contents().

var array = <?php echo file_get_contents($url) ?>;
console.log(array);
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