尝试执行 $.getJSON 调用时出现 404 not found 错误
getting 404 not found error while trying to do $.getJSON call
我检查了 link,它运行良好。这是我在控制台中遇到的错误:
GET http://localhost:60789/api.openweathermap.org/data/2.5/weather?q=Tel%20Aviv%2CIL&units=metric&APPID=the given number 404 (Not Found)
$(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);
})
});
});
您在 openWeatherMap
URL 上缺少 http://
前缀。因此,浏览器假定您提供的路径是相对于当前 URL 的路径,因此在其前面加上 http://localhost:60789/
- 因此您的 404.
要解决此问题,只需在 URL 前添加 http://
使其成为绝对值:
var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather';
我检查了 link,它运行良好。这是我在控制台中遇到的错误:
GET http://localhost:60789/api.openweathermap.org/data/2.5/weather?q=Tel%20Aviv%2CIL&units=metric&APPID=the given number 404 (Not Found)
$(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);
})
});
});
您在 openWeatherMap
URL 上缺少 http://
前缀。因此,浏览器假定您提供的路径是相对于当前 URL 的路径,因此在其前面加上 http://localhost:60789/
- 因此您的 404.
要解决此问题,只需在 URL 前添加 http://
使其成为绝对值:
var openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather';