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

133
Vistas
Do not display data output in ajax

I wrote the following code snippet for Ajax connections, but unfortunately the return value is not displayed in the output, but it does not give a special warning to understand the meaning. Please help.

js

$("#search").on('keyup', function(){var value = $(this).val();
  $.ajax('feed.php',{
    type: 'POST',
    dataType: 'json',
    data: {
      keyword: value
    },
    success: function(data){
      $("#pre").html(data);
    }
  });
});

feed.php

<?php
require_once('main.php');
$db = Db::getInstance();
$keyword = $_POST['keyword'];
$records = $db->query("SELECT * FROM dic_word WHERE word LIKE '%$keyword%'");

$out['html']= '';
foreach($records as $record){
  $out['html'] .= $record['word'] . '<br>';
}

echo json_encode($out);
?>

enter image description here

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

js:

jQuery('#search').on('keyup', () => {
    jQuery.ajax({
        url: 'feed.php',
        type: 'POST',
        data: { keyword: jQuery(this).val() },
        success: response => {
            jQuery('#pre').html(response);
        }
    });
});

feed.php

<?php
require_once('main.php');
$database = Db::getInstance();
$keyword = $_POST['keyword'];
$records = $database->query("SELECT * FROM dic_word WHERE word LIKE '%$keyword%'");
$output = '';
foreach($records as $record){
    $output .= $record['word'] . '<br />';
}
echo($output);
?>

PS: You don't need to use json output absolutly. But if there is coercion to using json output, the problem is 2 following items:

  1. You don't set the output "Content-Type" to json: header('Content-Type: application/json');
  2. You shouldn't pass the json object to html method in jQuery and should parsing it at first with JSON.parse(response) class, then with foreach, for or anything else process it
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