我正在尝试创建一个 Chrome 扩展程序,并尝试从 http://api.openweathermap.org/ 检索此扩展程序中的天气信息。地点。 我已在 list 文件中提供了必要的权限。 这是我的 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();
}
问题是代码没有进入任何就绪状态的 block 。 所以调用 send() 函数后就好像什么也没发生一样。
有什么想法吗?
这是我的扩展 list 文件权限部分。
"permissions":[
"http://api.openweathermap.org/data/*",
"geolocation"
]
最佳答案
好吧,我想我自己找到了答案。
JavaScript 区分大小写。我错误地将 readyState
写为 readystate
。
我真是太傻了。
谢谢大家。
关于javascript - Chrome 扩展 xmlHttpRequest 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24207063/