简体   繁体   中英

Chrome Extension xmlHttpRequest not working

I'm trying to create a chrome extension and I'm trying to retrieve the weather information in this extension from the http://api.openweathermap.org/ site. I have provided the necessary permissions in the manifest file. Here is my javascript code.

    function processPosition(pos)
{
    document.getElementById("testDiv").innerHTML = "Position available. lat=" + pos.coords.latitude  + "&lon=" + pos.coords.longitude;
   v_url = "http://api.openweathermap.org/data/2.5/weather?lat=" + pos.coords.latitude  + "&lon=" + pos.coords.longitude;

   if(window.XMLHttpRequest)
   {
      xmlhttp = new XMLHttpRequest();
   }
   else
   {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange = function()
   {
      if(xmlhttp.readystate == 4 && xmlhttp.status == 200)
      {
         processWeatherJSON(xmlhttp.responseText);
      }
      else if(xmlhttp.readystate == 3)
      {
        document.getElementById("testDiv").innerHTML = "Downloading data";        
      }
      else if(xmlhttp.readystate == 2)
      {
        document.getElementById("testDiv").innerHTML = "Send has been called";        
      }
            else if(xmlhttp.readystate == 1)
      {
        document.getElementById("testDiv").innerHTML = "Ready state 1";
      }
            else if(xmlhttp.readystate == 0)
      {
        document.getElementById("testDiv").innerHTML = "Ready state 0";        
      }
   }

   xmlhttp.open("GET",v_url,true);
   xmlhttp.send();
}

The problem is that the code does not enter the blocks for any of the readystates. So its like nothing has happened after the send() function was called.

Any ideas?

Here is my extension manifest file permissions section.

"permissions":[
        "http://api.openweathermap.org/data/*",
        "geolocation"
    ]

Alright I guess I found the answer myself. Javascript is case-sensitive. I wrongly wrote readyState as readystate .

Really silly of me.

Thanks to everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM