Bonjour,
Voila je rencontre un petit problème avec mon code.
Ce que je fais
Avec une requête Ajax j'essaye de récupérer des champs dans un db, mais je un drôle de retour , j'ai du mal ^^
$.ajax({
type: 'GET',
url: 'app/load.php',
success: function (data) {
console.dir(data);
$.each( data, function(i, contact ){
// var html = wrapHTml(contact); // wrapping commenté pour déboggage
// $contactsList.append(html);
});
},
error: function () {
alert('Récupération des donées impossible');
}
});
le load.php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "contact";
$tableName = "contact";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM contact";
$res = $db->query($sql);
$data = $res->fetchAll(PDO::FETCH_OBJ);
var_dump($data);
return $data;
Ce que je veux
C'est les infos mais non formater comme le retour ci-dessous.
Ce que j'obtiens
jquery.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in <pre class='xdebug-var-dump' dir='ltr'>
<small>D:\wamp64\www\contactApi\app\loadContact.php:25:</small>
<b>array</b> <i>(size=3)</i>
0 <font color='#888a85'>=></font>
<b>object</b>(<i>stdClass</i>)[<i>3</i>]
<i>public</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
<i>public</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'John Doe'</font> <i>(length=8)</i>
<i>public</i> 'photo' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'img/tux.png'</font> <i>(length=11)</i>
1 <font color='#888a85'>=></font>
<b>object</b>(<i>stdClass</i>)[<i>4</i>]
<i>public</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2'</font> <i>(length=1)</i>
<i>public</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'Bob L'�ponge'</font> <i>(length=12)</i>
<i>public</i> 'photo' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'img/bob.png'</font> <i>(length=11)</i>
2 <font color='#888a85'>=></font>
<b>object</b>(<i>stdClass</i>)[<i>5</i>]
<i>public</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3'</font> <i>(length=1)</i>
<i>public</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'Jean'</font> <i>(length=4)</i>
<i>public</i> 'photo' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'img/tux.pn'</font> <i>(length=10)</i>
</pre>
Et pourquoi quand je mets un dataType : 'json' j'ai l'alert Récupération des donées impossible' ? :/
merci
EDIT
je reçoit bien en retour de load.php un array avec les 4 enregistrements quand je fais un var_dump, mais ensuite dans app.js le callBack success de l'appel Ajax data est vide :/
json_encode ne donne rien non plus :s