【问题标题】:XMLHTTPRequest Request To Open Weather Returning Status 0XMLHTTPRequest 请求打开天气返回状态 0
【发布时间】:2018-04-03 00:31:24
【问题描述】:

标题几乎说明了问题所在。我已将 URL 输入到地址栏中,并且返回正常。我觉得它有点愚蠢,我只是看不到它,因为我已经盯着它看了一段时间,但这是代码。

(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)

    }
  }
})();

【问题讨论】:

  • 您还请求 JSON,但警告 responseXML?

标签: javascript xmlhttprequest openweathermap


【解决方案1】:

尝试指定完整路径:

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

否则,它会寻找[yourdomain]/api.openweathermap.org/...

【讨论】:

  • 该死的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2018-11-11
  • 1970-01-01
  • 2011-04-22
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多