Partage
  • Partager sur Facebook
  • Partager sur Twitter

Ajax Lecture d'une API JSON REST

    21 novembre 2017 à 17:02:47

    Bonjour à tous celà fait deux jours que je suis bloqué avec un script qui est censé lire une API (ce qu'il fait très bien) et la retourner dans un tableau

    <?php
    $json = file_get_contents("http://api.football-data.org/v1/fixtures/");
     $reqPrefs['http']['method'] = 'GET';
     $reqPrefs['http']['header'] = 'X-Auth-Token: d7016888969d4a30b6920842d09e9453';
    $data=array();
    $data = json_decode($json, true);
    
    ?><pre><?php print_r ($data);
    ?></pre>
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    	<title></title>
    
    	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    
    </head>
    <body>
    
    	<table id="matchs" class="table table-responsive table-striped">
    		<thead></thead>
    		<tbody></tbody>
    	</table>
    
    
    
    
    	<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    	<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    	<script type="text/javascript">
    		
    		$(document).ready(function(){
    			var thead = "<tr>"+
    							"<th>Home Name</th><th>Away Name</th><th>Score</th><th>HT Score</th><th>FT Score</th><th>ET Score</th><th>"+"Time</th><th>Status</th><th>Added</th><th>Last Changed</th><th>League Name</th>"+
    						"</tr>";
    			
    			
    			function worker() {
    			 	$.ajax({
    				    type: "GET",
    				    url: "matchs.php",
    	  				dataType: 'json',
    				    success: function(response){
    				        
    				        var matchs = response.data.match;
    				        console.log(matchs);
    				        displayData(matchs);
    				    },
    				    complete: function() {
    				      	setTimeout(worker, 5000);
    				    }
    				 });
    			}
    			worker();
    			function displayData(matchs)
    			{	
    				$('#matchs thead').html("");
    				$('#matchs thead').append(thead);
    				$('#matchs tbody').html("");
    				$.each(matchs, function(key,value)
    		        {
    		        	var match = value;
    		        	
    		        	var tr = "<tr><td>"+match.homeTeamName+"</td><td>"+match.awayTeamName+"</td><td>"+match.goalsHomeTeam+"</td><td>"+match.goalsAwayTeam+"</td></tr>";
    		        	
    		        	$('#matchs tbody').append(tr);
    		        });
    			}	
    		});
    	</script>
    </body>
    </html>
    

    Je ne trouve pasde solution c'est pour cela que je viens vers vous. Merci beaucoup de votre aide.


    • Partager sur Facebook
    • Partager sur Twitter

    Ajax Lecture d'une API JSON REST

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown