2016-06-13 24 views
0

我做的https://www.freecodecamp.com/challenges/show-the-local-weather

項目我整個項目是在這裏,如果你需要:https://codepen.io/tugrulz/pen/beEmJb?editors=0010

function fetchWeather() { 

    var ap = "http://api.openweathermap.org/data/2.5/weather?"; 
    var key = "&appid=061f24cf3cde2f60644a8240302983f2"; 
    var lat = "35"; 
    var lon = "139"; 
    var api = ap + "lat=" + lat + "&lon=" + lon + key; 

    $(".location").html(api); 

$.getJSON(api, function(data) { 
     alert("sa"); 
     $(".location").html("oldu mu?"); 
    }) 
    .done(function() { 
    alert("second success"); 
    }) 
    .fail(function(error) { 
    alert(error); 

    }) 
    .always(function() { 
    alert("complete"); 
    }); 
} 

此打印:http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=061f24cf3cde2f60644a8240302983f2

問題是,我打印的鏈接正確地在chrome中工作,但getJSON不起作用。它的失敗功能起作用。

我該怎麼辦? 和任何想法如何打印錯誤信息?打印(誤差);沒有幫助。無論是涉及getJson的try和catch語句。

+0

你確定你叫'fetchWeather()'。看看這個[CodePen](http://codepen.io/anon/pen/aZNEgz)。它基本上是你的代碼,我將響應名稱寫入一個div。所以它工作。 – KRONWALLED

+0

是的,我可以,但它已經準備就緒,我使用引導程序,但是當我改變它們時,沒有一個有效。有趣。我會用你的模板作爲模板,但我仍然很好奇項目之間的區別。 – TugRulz

+0

我真的不知道我們的2個版本之間有什麼區別。我更新了我的筆以在'$(document).ready()'中包含'fetchWeather()'調用,並且它仍然有效。 – KRONWALLED

回答

0

我跑你codepen項目,並在我的控制檯我得到了錯誤

The page at 'https://codepen.io/tugrulz/pen/beEmJb?editors=0011' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=061f24cf3cde2f60644a8240302983f2'. This request has been blocked; the content must be served over HTTPS. 

Codepen擋住了API,因爲它不是一個https地址。它不會打印錯誤,因爲請求甚至不會發送到天氣服務。

+0

那我該怎麼辦?寫https不起作用 – TugRulz

相關問題