Does Ajax only bring results on the console?

Asked

Viewed 83 times

1

Good afternoon guys, I can bring a result in the log, but in the div is not passed on, follows my source:

Index.php

var teste;
$.ajax({
    url: 'conversa.php',
    type: 'GET',
    success: function(res) {
        $("#conversa").html(res);
        teste = $("#conversa").html(res);
        console.log(document.getElementById("conversa"));
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

php conversation.

<?php
include '../../abreConexao.php';
session_start();

$query = "SELECT * FROM chat WHERE meu_id='" . $_SESSION['id-cadastro'] . "' AND parceiro_id='" . $_SESSION['paquera_chat'] . "' OR meu_id='" . $_SESSION['paquera_chat'] . "' AND parceiro_id='" . $_SESSION['id-cadastro'] . "' ORDER BY data_hora ASC";
$result = mysql_query($query);
while ($fetch = mysql_fetch_array($result)) {
    $variavel .= '{"_id":"' . $fetch['parceiro_id'] . '","text":"' . $fetch['mensagem'] . '","userId":"' . $fetch['meu_id'] . '","date":"' . $fetch['data_hora'] . '"},';
}
echo $variavel; 	
?>

Console

I just really wanted to take the select of the.php conversation and display it in index via ajax. I’ll be waiting, guys, thank you.

--Solved

$.ajax({ 
		url: 'conversa.php', 
		type: 'POST', 
		success: function(res){ 
		  $("#conversa").empty();
		  $("#conversa").append(res);
		  teste = document.getElementById('conversa').innerHTML; 
		  console.log(document.getElementById("conversa"));
		}
	  });

  • 1

    What exactly do you want to display within the div? The answer is coming from an object with various information!

  • I really want to show off.

  • So, but for you to do this, I think you’ll have to make a loop to add each element in the div

  • exactly as it is on the console

  • because it looks echo on my php page looks like this:{"_id":"57","text":"hi mom","userid":"54","date":"2016-10-19T13:45:00.000Z"},{"_id":"54","text":"Hi son","userid":"57","date":"2016-10-19T13:47:00.000Z"},

  • Try the following: $("#conversation"). html(JSON.stringify(res)); see if the whole object appears inside the div

  • Try using . append(res) instead of . html(res)

  • so I used append, but when I give Object Object Alert

  • $("#conversation"). html(JSON.stringify(res)) also appeared nothing

  • I did one more test here you guys & #Xa;$("#conversation"). append(res); it worked Muitoooo thanks.

  • Put the result as a response and, after two days, mark it as the right answer.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.