【问题标题】:Chrome Extension xmlHttpRequest not workingChrome 扩展 xmlHttpRequest 不起作用
【发布时间】:2014-06-13 13:54:22
【问题描述】:

我正在尝试创建一个 chrome 扩展程序,并且我正在尝试从 http://api.openweathermap.org/ 站点检索此扩展程序中的天气信息。 我在清单文件中提供了必要的权限。 这是我的 JavaScript 代码。

    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();
}

问题是代码没有进入任何就绪状态的块。 所以在调用 send() 函数之后就像没有发生任何事情。

有什么想法吗?

这是我的扩展清单文件权限部分。

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

【问题讨论】:

  • 如果是readystate == 4 && status !== 200呢?

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

好吧,我想我自己找到了答案。 Javascript 区分大小写。我错误地将readyState 写成readystate

我真的很傻。

谢谢大家。

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2013-12-24
    • 2012-01-22
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    相关资源
    最近更新 更多