Criando Elemento com o retorno php
Galera, é o seguinte!
tenho um código que faz uma requisição ajax em uma pagina php , que faz um consulta e retorna os dados em json:
Com o resultado, gostaria de criar uma listagem com o resultado.
como percorrer o resultado via javascript? Se possivel, gostaria que essa função me retornasse o json...mais não tive sucesso!
Código da função
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Get Data </title>
</head>
<body>
</body>
</html>
<script>
function getData(url,query){
var ajax = null;
if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}else if (window.ActiveXObject){
try{
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
}
ajax.onreadystatechange = function(){
if(ajax.status >= 200 && ajax.status <= 400){
resultadoGetData = ajax.responseText;
}
}
if(query != undefined){
url = url + "?query=" + query;
};
ajax.open('POST',url,true);
ajax.send();
//return ajax.onreadystatechange();
return resultadoGetData;
}
var resultado = getData('motor-get-data.php','SELECT * FROM tbl_caixa');
</script>
Código da pagina php
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: application/json; charset=utf-8');
$database = '@rdmoveisplanejados';
$query = $_GET['query'];
try{
$conn = new PDO("mysql:host=localhost;dbname=$database", "root", "");
$sql = $conn->prepare($query);
$sql -> execute();
$cont = $sql->rowCount();
$row = $sql->fetchAll(PDO::FETCH_OBJ);
echo json_encode($row);
}catch(PDOException $e){
echo "Falha:001<br>";
echo "Erro:" . $e->getMessage();
exit;
}
AgradeçoDiscussão (1)
Carregando comentários...