Ajax RSS提要-OpenWeatherMap

so I get the feed after choosing an option but its not "refreshing as new news come". I have the same problem with openweather. Is the "GET" my problem or browser or something else? I guess everything is cached but it should not. Should it be POST and not GET how sould I change that with the current code? thanks!

<script>

nocache = "&nocache" + Math.random()* 1000000
url = "$q"
out = "";


function showRSS(str) {
  if (str.length==0) {
    document.getElementById("rssOutput").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("rssOutput").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","getrss.php?q="+str+nocache,true);
  xmlhttp.send();
}

</script>

<?php
//get the q parameter from URL
$q = $_GET["q"];

//find out which feed was selected
if($q=="Baden-Württemberg") {
  $xml=("https://verkehrsmeldungen.polizei-bw.de/TicRss.ashx?region=BW");
} elseif($q=="Bayern") {
  $xml=("http://www.br.de/verkehr-static/verkehrsmeldungen-rss.xml");
}

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
echo("<p ><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=5; $i++) {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;
  echo ("<p ><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br>");
  echo ($item_desc . "</p>");
}

    ?>

From what you are saying or trying to accomplish, is to either use

  • polling
  • event or socket connection (socket.io)

Now with Polling, you set a particular interval to always go and check for new updates. This path is not too efficient as you might go to the server to retrieve new information and there might be none returned as at the time you sent the request.

Sample

var interval = 3000// milliseconds
function fetchFeeds(){

    setTimeout(function(){
        // goto the server here and manage returned record
    }, interval);
 }

For Events: you can check up on server side events here but for only HTML5, but nice stuff.

Will advise to go for socket instead, check socket.io