为什么我的xmlhttprequest()。状态404即使url存在? [英] Why is my xmlhttprequest().status 404 even though the url exists?

查看:489
本文介绍了为什么我的xmlhttprequest()。状态404即使url存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将天气应用程序构建为FreeCodeCamp的项目。当我在浏览器中输入我的网址时,它会输出数据。但是,即使我的readyState是4,我的状态仍然是404.



这是代码。

I am trying to build a weather app as a project for FreeCodeCamp. When I type in my url in my browser, it outputs the data. However, my status is still 404 even though my readyState is 4.

This is the code.

var APPID = "85b5c033328a263ce0ee20b8a0b4ac30";
var temperature;
var weather;
var windSpeed;
var direction;

function getData(latitude, longitude) {
    var url = "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=" + APPID + "&callback=JSON_CALLBACK";
    sendRequest(url);
}

function sendRequest(url) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        console.log("state " + xmlhttp.readyState);
        console.log("status " + xmlhttp.status);
        console.log(url);
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var data = JSON.parse(xmlhttp.responseText);
            weather = {};
            weather.name = data.name;
            weather.temperature = data.main.temp;
            weather.wind = data.wind.speed;
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

function update(weather) {
    temperature.innerHTML = weather.temperature;
}

window.onload = function() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var lat = position.coords.latitude;
            var long = position.coords.longitude;
            getData(lat, long);
        });
    }

}







我尝试了什么:



我尝试将& callback = JSON_CALLBACK更改为& mode = xml但它仍然输出404.




What I have tried:

I have tried changing my "&callback=JSON_CALLBACK" to "&mode=xml" but it still outputted 404.

推荐答案

读取状态4表示您的响应是404(未找到)。它告诉你 api.openweathermap.org 的服务器正在运行(404答案来自那里),但URL的本地部分无效。



您是否看过 xmlhttp.responseText 中的404消息?

它应该给你一个关于哪个部分的提示无效。



根据当前天气数据 - OpenWeatherMap [ ^ ]默认格式为Jason和回调定义了一个必须实现的JavaScript回调函数。因此,不需要使用回调模式参数。



您也可以只使用 lat lon 参数进行尝试,并且 - 因为它有效在浏览器中 - 使用在浏览器中输入的URL进行测试(即将 url 变量设置为该固定字符串)。



尝试使用小写 appid 参数(参数可能区分大小写)。



另请参见此支持主题(特别是Ivan 2015年9月15日03:51 UTC的帖子): OpenWeatherMap |错误:未找到城市 [ ^ ]
Read state 4 indicates that you got an response which is 404 (not found) in your case. It tells you that the server at api.openweathermap.org is working (the 404 answer is from there) but that the local part of the URL is invalid.

Did you had a look at the 404 message in xmlhttp.responseText?
It should give you a hint about which part is invalid.

According to Current weather data- OpenWeatherMap[^] the default format is Jason and callback defines a JavaScript call back function which must be implemented. So there should be no need to use the callback or mode parameter.

You may also try it with just the lat and lon parameters, and - because it works in the browser - by using the URL entered in the browser for testing (that is setting your url variable to that fixed string).

Try also using a lower case appid parameter (parameters may be case sensitive).

See also this support thread (especially the post from Ivan Sep 15, 2015 03:51PM UTC): OpenWeatherMap | Error: Not found city[^]


我找到了解决方案。在URL中,我需要将http://添加到网址的前面。
I figured out the solution. In the URL I needed to add http:// to the front of the url.


这篇关于为什么我的xmlhttprequest()。状态404即使url存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆