【问题标题】:getting 404 not found error while trying to do $.getJSON call尝试进行 $.getJSON 调用时出现 404 not found 错误
【发布时间】:2016-12-30 13:43:30
【问题描述】:

我检查了链接,它运行良好。这是我在控制台中遇到的错误:

GET http://localhost:60789/api.openweathermap.org/data/2.5/weather?q=Tel%20Aviv%2CIL&units=metric&APPID=the 给定数字 404(未找到)

$(document).ready(function () {
    var getIP = 'http://ip-api.com/json';
    var openWeatherMap = 'api.openweathermap.org/data/2.5/weather'

    $.getJSON(getIP).done(function(location) {
        $.getJSON(openWeatherMap, {
            q: location.regionName + "," + location.countryCode,
            units: 'metric',
            APPID: 'Here iam giving my appid'
        }).done(function (weather) {
                console.log(weather);                
            $('ul:first-child').html(weather.name + "," + weather.sys.country);
        })
    });
});

【问题讨论】:

  • 那个 GET 请求清楚地表明了问题......有人没有使用绝对链接。

标签: javascript jquery ajax frontend


【解决方案1】:

您在openWeatherMap URL 上缺少http:// 前缀。因此,浏览器假定您提供的路径是相对于当前 URL 的,因此在其前面加上 http://localhost:60789/ - 因此是 404。

要解决此问题,只需将http:// 附加到 URL 以使其绝对:

var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather';

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2019-08-02
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多