【问题标题】:Error 405 & 401 open weather map API Angularjs错误 405 和 401 打开天气图 API Angularjs
【发布时间】:2017-12-12 00:26:32
【问题描述】:

试图通过openweathermap api调用数据 如果我通过'GET'方法调用它。有

405(不允许的方法)

var req = {
    method: 'GET',
    url: 'http://api.openweathermap.org/data/2.5/forecast/daily?APPID=' + ApiKey + '&q=London,us',
    headers: {
        'x-api-key': ApiKey
    }
}

$http(req)
    .then(function (data) {
        console.log(data);
    }, function (err) {
        console.log(err);
    });

【问题讨论】:

  • 你使用的是哪个版本的角度?
  • 试试 $http.get('api.openweathermap.org/data/2.5/forecast/daily?APPID=' + ApiKey + '&q=London,us');
  • 试试$http.get(URL,{header}).then(function(response){}).catch(function(err){})
  • @anand,它的 1.6+
  • @HimeshSuthar 仍然 401 错误

标签: angularjs openweathermap


【解决方案1】:

使用 params 属性对搜索参数进行编码:

var req = {
    method: 'GET',
    //url: 'http://api.openweathermap.org/data/2.5/forecast/daily?APPID=' + ApiKey + '&q=London,us',
    url: 'http://api.openweathermap.org/data/2.5/forecast/daily',
    params: { appid: ApiKey, q: 'London,us' }
}

问题很可能是&q=London,us 是非法的。逗号字符需要为percent-encodedq=London%2Cus。使用params 属性正确编码参数。


更新

我用我的 APPID(我不打算发布)对其进行了测试,它成功了。

这是我的 DEMO on PLNKR 删除了 APPID。

【讨论】:

  • 现在是 401 (Unauthorized) ,使用生成的 apikey & 仅使用伦敦
  • 是的,我已经注册并生成了一个新的 api 但仍然是同样的问题
  • 我添加了截图
  • 你能分享你的代码吗只是XXX你的appid,你认为是因为localhost吗?
  • 好的,我更新了,根据您的代码,只有“伦敦”,现在错误是“XMLHttpRequest 无法加载api.openweathermap.org/data/2.5/forecast/…。预检响应的 HTTP 状态代码 405 无效”
【解决方案2】:

@faisal 我今天遇到了这个错误,经过一些调试,我意识到这是因为我的配置文件中有$httpProvider.defaults.headers.common['X-Requested-With'] = 'HttpRequest';。为了方便起见,我只是禁用了 CORS 并解决了问题。

这个答案对我有帮助:Disable CORS in angularJS

【讨论】:

  • 这是个很老的问题,非常感谢您的帮助,让我检查一下非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
相关资源
最近更新 更多