Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
JavaScript
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);
        });
    }

}



What I have tried:

I have tried changing my "&callback=JSON_CALLBACK" to "&mode=xml" but it still outputted 404.
Posted
Updated 21-Nov-16 18:15pm

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[^]
 
Share this answer
 
Comments
Member 12861628 21-Nov-16 7:19am    
Thanks for your reply but when I entered my url into my browser, I get this response

{"coord":{"lon":96.16,"lat":16.81},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"base":"stations","main":{"temp":300.15,"pressure":1009,"humidity":78,"temp_min":300.15,"temp_max":300.15},"visibility":7000,"wind":{"speed":2.6,"deg":350},"clouds":{"all":40},"dt":1479727800,"sys":{"type":1,"id":7988,"message":0.0066,"country":"MM","sunrise":1479685376,"sunset":1479725982},"id":1298824,"name":"Rangoon","cod":200}

I know its not a URL error, so something must be wrong with the code itself.
Jochen Arndt 21-Nov-16 7:30am    
You posted the reply shown in your browser.
But you should show (and hard code for testing) the URL entered into the browser's URL field.
Member 12861628 21-Nov-16 22:06pm    
This is the url:
http://api.openweathermap.org/data/2.5/weather?lat=16.84665464876105&lon=96.11747870679461&APPID=85b5c033328a263ce0ee20b8a0b4ac30
Jochen Arndt 22-Nov-16 2:46am    
And you found the solution yourself:
You missed the http::/ part.
I figured out the solution. In the URL I needed to add http:// to the front of the url.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900