user3390812
user3390812

Reputation: 15

XMLHTTPRequest Request To Open Weather Returning Status 0

Title says pretty much the problem. I've entered the URL into my address bar and it comes back fine. I feel like its something stupid I just can't see because I've been staring at it for awhile, but here's the code.

(function() {
  var httpRequest;
  document.getElementById("weatherButton").addEventListener('click', makeRequest);
  function makeRequest() {
    httpRequest = new XMLHttpRequest();
    if (!httpRequest) {
      alert('Cannot create XMLHTTP instance!');
      return false;
    }
    httpRequest.onreadystatechange = alertContents;
    httpRequest.open("GET", "api.openweathermap.org/data/2.5/weather?zip=94040&APPID=xxxxxxxxx&mode=json");
    httpRequest.send();
  }
  function alertContents() {
    if (httpRequest.readyState != 4) {
      return;
    }
    if (httpRequest.status == 200) {
      alert(httpRequest.responseXML);
    }
    if (httpRequest.status != 200) {
      alert(httpRequest.status + ": " + httpRequest.statusText_)
      alert(httpRequest.readyState)

    }
  }
})();

Upvotes: 1

Views: 528

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370789

Try specifying the full path:

httpRequest.open("GET", "https://api.openweathermap.org/data/2.5/weather?zip=94040&APPID=xxxxxxxxx&mode=json");

Otherwise, it looks for [yourdomain]/api.openweathermap.org/....

Upvotes: 2

Related Questions