Bonjour,
J'essaie d'afficher une image via une requête ajax dans le but de la rafraichir régulièrement. Je me suis grandement inspiré de cette discussion.
J'ai donc une page principale test.php
et une page get-image.php qui récupère l'image chez Yahoo finance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 go('day', 'ATO.PA'); function createRequestObject() { var http; if (window.XMLHttpRequest) { // Mozilla, Konqueror/Safari, IE7 ... http = new XMLHttpRequest(); } else if (window.ActiveXObject) { // Internet Explorer 6 http = new ActiveXObject("Microsoft.XMLHTTP"); } return http; } // createRequestObject() function go(graphique, quote) { var url = 'get-image.php?img=' + graphique + '&action=' + quote; var http = createRequestObject(); http.open('GET', url); http.onreadystatechange = (function () { if (http.readyState == 4) { if (http.status == 200) { //var rep = http.responseText; //alert(rep); var j = document.getElementById('image'); document.getElementById('image').src = 'get-image.php' //document.getElementById('idFrame').src = "autreFichier.html"; var img = document.createElement('img'); img.src = 'get-image.php'; //img.scr = xhr.responseText; j.appendChild(img); //noeud enfant } else { alert('Pas glop pas glop'); } } }); http.send(null); } // go() .../... <div id="image"> </div>
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <?php //$quote = $_GET['action']; // marche pas $quote = 'ATO.PA'; // ok //$graph = $_GET['img']; // marche pas $graph = 'day'; //ok if ($graph == "day") $img = 'http://chart.finance.yahoo.com/t?s=' . $quote . '&lang=fr-FR®ion=FR&width=300&height=180'; if ($graph == "week") $img = 'http://chart.finance.yahoo.com/v?s=' . $quote . '&lang=fr-FR®ion=FR&width=300&height=180'; if ($graph == "year") $img = 'http://chart.finance.yahoo.com/c/0b/a/' . $quote . '?lang=fr-FR®ion=FR&width=300&height=180'; $info = getimagesize($img); header('Content-Type: '.$info['mime']); echo file_get_contents($img); exit; ?>
Le problème est que la requête AJAX n'a pas l'air d'envoyer les paramètres img et action à get-image.php.
Si je fixe ces 2 paramètres paramètres dans get-image.php, je récupère bien l'image dans la page principale.
Si j'ouvre le lien get-image.php?img=day&action=ALT.PA, l'image est récupérée.
J'ai l'exemple en ligne ici.
Merci.
Partager