只做一些 JQuery AJAX,逐步进行,但我不断收到 404 错误。

huangapple go评论69阅读模式
英文:

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 //.

huangapple
  • 本文由 发表于 2023年4月4日 04:21:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定