Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

91
Vistas
Problem parsing through Ajax a JSON data fetched by PHP file

I have difficulty parsing JSON date from my PHP file

{"date":"20\/12\/2022","result":"£13000.00","medias":"BBC","country":"UK"}

but when I try to parse it and to see the data in the console.log - it's empty

Please help

My Ajax Function

function ajax_call(){
    const style2 = $("#style1").val();
    const radio_btn = $('input[name="drone"]:checked').val();
    if(style2==""){
        document.getElementById("error").innerHTML = "Error: Please enter style code !"; 
        return false;
    } 
    {
        $.ajax({ 
            type: 'post',
            url: "t.php",  
            data: { styles: style2 , country: radio_btn},
            dataType: "json",
            success: function(data){  
                var jsondata = $.parseJSON(data);
                console.log(jsondata);
            }
        })
    }
}

My PHP

<php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  
                    'result' =>iconv('Windows-1252', 'UTF-8', $end_result), 
                    'medias' => $medias, 
                    'country' => $country
            );

echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);
?>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I believe there is a typo in the PHP code, you need to change the first line from:

<php

to

<?php

If the first line is <php, the output won't be valid JSON.

Complete PHP code:

<?php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  'result' =>iconv('Windows-1252', 'UTF-8', $end_result), 'medias' => $medias, 'country' => $country);


echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);

?>

Also, I'm not 100% sure you need the iconv call, you could try the below code:

<?php

header('Content-type: application/json');
$date = "20/12/2020";
$end_result = "£13000.00";
$medias = "BBC";
$country = "UK";

$sortjson = array('date' => $date,  'result' => $end_result, 'medias' => $medias, 'country' => $country);

echo json_encode($sortjson, JSON_UNESCAPED_UNICODE);

?>
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to use JSON.parse()

function ajax_call(){
const style2 = $("#style1").val();
const radio_btn = $('input[name="drone"]:checked').val();
if(style2==""){document.getElementById("error").innerHTML = "Error: Please enter style code !"; return false;} {
$.ajax({ 
    type: 'post',
    url: "t.php",  
    data: { styles: style2 , country: radio_btn},
                  dataType: "json",
                 success: function(data){  
   var jsondata = JSON.parse(data);
        console.log(jsondata);
}
})
}
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have a error in your Success method:

use JSON.parse() method instead of $.parseJSON()

success: function(data){  

   var jsondata = JSON.parse(data);
   console.log(jsondata);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda