2014-06-13 86 views
1

我想創建一個Chrome擴展,並試圖從http://api.openweathermap.org/網站檢索此擴展中的天氣信息。 我已經在清單文件中提供了必要的權限。 這是我的javascript代碼。Chrome擴展xmlHttp請求不工作

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

問題是代碼沒有輸入任何readystate的塊。 所以它在調用send()函數後沒有發生任何事情。

任何想法?

這是我的擴展清單文件權限部分。

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

如果它是'readystate == 4 && status!== 200'會怎麼樣? – abraham

回答

1

好吧我想我自己找到了答案。 JavaScript區分大小寫。我錯誤地寫了readyState作爲readystate

真的很愚蠢的我。

感謝大家。