英文:
Just doing some JQuery AJAX and I'm going step by step, but I keep getting a 404
问题
我只会翻译您提供的代码部分:
$(document).ready(function() {
$(".search button").click(function() {
getWeather($(".search-bar").val());
});
});
function getWeather(city) {
var metric = "&units=metric";
var myAPI = "4f41b20d31ef924373402ebf9b9fe310";
var url = "api.openweathermap.org/data/2.5/forecast?q=" + city + metric + "&appid=" + myAPI;
$.getJSON(url, function(data) {
console.log(data);
});
}
如果您有其他需要翻译的内容,请告诉我。
英文:
I'm just trying to test out JQuery AJAX and I'm going about it slowly. I open up the file on browser and use chrome dev tools to find find out what's going on, and it says that the status code is 404. This tells me that my GET is going nowhere? Code below:
$(document).ready(function()
{$(".search button").click(function()
{getWeather(
$(".search-bar").val()
)}
)}
);
function getWeather(city) {
var metric = "&units=metric";
var myAPI = "4f41b20d31ef924373402ebf9b9fe310";
var url="api.openweathermap.org/data/2.5/forecast?q=" + city + metric + "&appid=" + myAPI;
$.getJSON(url, function(data)
{
console.log(data);
})
}
I checked my url and everything but I keep getting the same thing. Is there something wrong with my code so far? It's a simple code right now but I don't know what's going wrong.
答案1
得分: 0
You're missing the root of the URL:
var url = "//api.openweathermap.org/data/2.5/forecast?q=" + city + metric + "&appid=" + myAPI;
// ^-- here
Otherwise, when you tell the browser to make a request to api.openweathermap.org
, structurally that's no different than making a request to index.html
or any other resource on your own server. Without specifying //
to indicate that this is a different host, the browser is going to request this from the current host relative to the current page, which may become something like:
https://localhost:12345/mypracticeproject/api.openweathermap.org/data/2.5/forecast?q=...
Which probably isn't what you wanted. You don't need to include the full protocol (http
or https
), but do at least need to include //
.
英文:
You're missing the root of the URL:
var url="//api.openweathermap.org/data/2.5/forecast?q=" + city + metric + "&appid=" + myAPI;
// ^-- here
Otherwise, when you tell the browser to make a request to api.openweathermap.org
, structurally that's no different than making a request to index.html
or any other resource on your own server. Without specifying //
to indicate that this is a different host, the browser is going to request this from the current host relative to the current page, which may become something like:
https://localhost:12345/mypracticeproject/api.openweathermap.org/data/2.5/forecast?q=...
Which probably isn't what you wanted. You don't need to include the full protocol (http
or https
), but do at least need to include //
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论