2017-05-04 1 views
0

abrufen Ich bin neu in JSON und Javascript. Ich möchte ein Stück Daten, die als JSON unter der URL: https://api.apixu.com/v1/forecast.json?key=353b879d4d984fcbba772116170405&q=Delhi&days=7 zur Verfügung steht, nachdem der Benutzer auf eine Schaltfläche auf meiner Website klickt. Ich war in der Lage, dass mit PHP zu tun, aber das ist zu viele API-Aufrufe für die JSON geben, so will ich es auslösen, wenn Benutzer auf die Schaltfläche klickt, dhWie können wir Daten aus einem JSON mit onClick-Ereignis

der Code wird wie folgt

<?php 

$url  = "https://api.apixu.com/v1/forecast.json?key=353b879d4d984fcbba772116170405&q=Delhi&days=7"; 
$response = file_get_contents($url); 


$response = json_decode($response, true); 



// data extraction for current day 
$city    = $response['location']['name']; // name of the city 
$country   = $response['location']['country']; // name of the country 
$max_temp_current = $response['current']['temp_c']; // current temprature of the day : pls note it is not the max temprature of the day 
$min_temp_current = $response['forecast']['forecastday'][0]['day']['mintemp_c']; // minumum temprature of the day 
$icon_current  = $response['current']['condition']['icon']; // to get the icon for current condition; 
$condition_current = $response['current']['condition']['text']; 
//$condition_current=$response['forecast']['forecastday'][0]['day']['condition']['text']; // current condition as given by the JSON 
$date_current1  = $response['forecast']['forecastday'][0]['date']; // Current Date of the day 
$d     = strtotime($date_current1); // this probably converts the day to second count from year 1970, pls see the PHP manual for confirmation. 
$date_current  = date('l j F Y', $d); // format the day in the form of dayName dd-monthName-yyyy. eg- Wednesday 26 April 2017 
$date_day_current = date('l', $d); // format the day for just the name of the day like Wednesday, Thursday etc. 
$date_today  = date('j F Y', $d); // format the day to form of dd-Month-yyyy. eg-26 April 2017 
?> 

Wenn Klickt der Benutzer auf die Schaltfläche, erhält er eine Alarmbox mit der maximalen Temperatur von Delhi für den Tag. mit PHP können wir Maximaltemp mit dem Code-

  $max_temp_current = $response['current']['temp_c']; 

Ich bin nicht in der Lage holen um es zu holen Java Skript und übergibt es Feld aufmerksam zu machen.

+1

können Sie bitte diese Geige versuchen. Ich machte eine Ajax-Anfrage, um diese Daten zu holen. Http://jsfiddle.net/4q95k4zv/ –

+0

@NairAthul stellen Sie es als eine Antwort, wenn das eine Antwort ist –

Antwort

0
 


            
 
function displayresult(){ 
 
    var xhttp = new XMLHttpRequest(); 
 
    xhttp.onreadystatechange = function() { 
 
    if (this.readyState == 4 && this.status == 200) { 
 
    myObj = JSON.parse(this.responseText); 
 
    document.getElementById("result").innerHTML = myObj.location.name; 
 
    // you can get all fields with myObj variable. 
 
    } 
 
    }; 
 
    xhttp.open("GET", "https://api.apixu.com/v1/forecast.json?key=353b879d4d984fcbba772116170405&q=Delhi&days=7", true); 
 
    xhttp.send(); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<body> 
 
<button type="button" onclick="displayresult()">Change Content</button> 
 
<div id="result"> 
 
</div> 
 
</body> 
 
</html>
0

können Sie xmlHttpRequest- verwenden

function loadXMLDoc() { 
 
    var xhttp = new XMLHttpRequest(); 
 
    xhttp.onreadystatechange = function() { 
 
    if (this.readyState == 4 && this.status == 200) { 
 
     document.getElementById("demo").innerHTML = 
 
     this.responseText; 
 
    } 
 
    }; 
 
    xhttp.open("GET", "https://api.apixu.com/v1/forecast.json?key=353b879d4d984fcbba772116170405&q=Delhi&days=7", true); 
 
    xhttp.send(); 
 
}
<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<h2>Using the XMLHttpRequest Object</h2> 
 

 
<div id="demo"> 
 
<button type="button" onclick="loadXMLDoc()">Change Content</button> 
 
</div> 
 

 
</body> 
 
</html>

Verwandte Themen