I am working on my college project, In that i have a webpage which is supposed to display City and temperature, I am trying to use AJAX to update temperature at some intervals. Here's the code
for ajax request Java Script, I saw this code in this answer https://stackoverflow.com/a/18243161/4341530
function postAjaxRequest(qry){
alert("Ajax response on " +qry);
$.ajax({
type: "POST",
url: "ajax_resp.php",
data: {q: qry},
dataType:'JSON',
success: function(response){
dispatchCallBack(qry,response);
}});}
the alert("Ajax response on " +qry);
is working fine so I know this code is running. But when I put this kind of "alert()" just before dispatchCallBack(qry,response);
its not working, so I infer here that the request is not successful.
Here's ajax_resp.php
<?php
if(isset($_POST['q'])&&!empty($_POST['q'])
{
if($_POST['q']=="c_temp")
{
$api_key = "Here's my api key, working alright";
$city_id = "here's city id, also working correct";
$url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;
$json = file_get_contents($url);
$json_o = json_decode($json);
$city_name = $json_o->city->name;
$temp_c = $json_o->list[0]->main->temp-273;
echo json_encode(array("city"=>$city_name,"temp"=>$temp));
//I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
}
}?>
so I can't figure out what's wrong. I am newbie :p May be some silly mistake, would be greatfull if pointed out.