2016-07-01 2 views
0
var weather; 
$(document).ready(function(){ 
alert("wellco"); 
var canvas = document.createElement('canvas'); 
canvas.id  = "CursorLayer"; 
canvas.width = 1224; 
canvas.height = 768; 
canvas.style.zIndex = 8; 
canvas.style.position = "absolute"; 
canvas.style.border = "1px solid"; 
document.body.appendChild(canvas); 

//the part where things go wrong begins 
data = $.getJSON('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c5959a33372923c74fccc1d07ab4b37b&units=metric'); 
weather=JSON.parse(data.responseText); 
console.log(weather.main.temp); 
if(data!="undefined"){ 
$('#temp').textContent = weather.main.temp; 
} 
}); 

그래서 chromeExtension을 작업 중이며 개방형 날씨를 사용하여 도시의 날씨를 얻습니다. 문제는 내가 getJSON을 사용하여 추측 한 데이터 값을 할당 할 때 시작됩니다. 이 오류가 발생합니다. : Uncaught SyntaxError : JSON의 위치 0에서 예기치 않은 토큰 uOpen Weather API는 JSON 데이터를 사용합니다.

여기 내 데이터 변수가 정의되지 않았다는 것을 알게되었습니다. 여기서 내가 뭘 잘못하고 있니?

답변

0

$.getJSON()은 비동기식입니다. 시도한대로 함수 호출의 결과로 값을 리턴하지 않습니다. 대신 반환 된 데이터를 처리하는 콜백 함수를 제공하십시오. 마지막으로 JSON은 이미 객체로 파싱되어 있습니다.

이 시도 :

$.getJSON('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c5959a33372923c74fccc1d07ab4b37b&units=metric', function (weather) { 
    console.log(weather.main.temp); 
}); 
관련 문제