简体   繁体   中英

Im having trouble logging JSON data to the console

my api link works fine when i copy and paste it into a tab, but my jquery code to get the JSON data and log it into the console does not work. replacing http with https doesn't work either.

$(document).ready(function(){

var api = "http://api.openweathermap.org/data/2.5/weather?
q=London&APPID=<myAppId>"
$.getJSON(api, function(data){
console.log(data.weather.main);

});
});

Try the $.ajax instead to get the error message, in the following way:

    var api = "http://api.openweathermap.org/data/2.5/weather?q=London&APPID=<myAppId>"
    $.ajax({
        url: api,
        type: 'GET',
        dataType: 'json',
    })
    .done(function(data) {
        console.log("success", data);
    })
    .fail(function(a,b,c) {
        console.log("error");
        console.log(a+b+c);
    })
    .always(function() {
        console.log("complete");
    });

The .fail function will bring you back the error message, in case it's server sided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM