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

141
Visualizações
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 Respostas
Responde à pergunta

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 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