热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

错误405&401开放天气地图APIAngularjs。-Error405&401openweathermapAPIAngularjs

tryingtocalldatathroughopenweathermapapiifIcallitthroughGETmethod.thereis尝试通过openwe

trying to call data through openweathermap api if I call it through 'GET'method.there is

尝试通过openweathermap api调用数据,如果我通过“GET”方法调用它。有

405 (Method Not Allowed)

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);
    });

enter image description here

2 个解决方案

#1


1  

@faisal I ran into this error today, and after some debugging, I realized that it was because I had $httpProvider.defaults.headers.common['X-Requested-With'] = 'HttpRequest'; in my config file. I just disabled CORS for convenience and solved the problem.

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

This answer is what helped me: Disable CORS in angularJS

这个答案帮助了我:禁用angularJS中的CORS。

#2


0  

Use the params property to encode the search parameters:

使用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' }
}

The problem is likely that the &q=London,us is illegal. The comma character needs to be percent-encoded to q=London%2Cus. Use the params property to properly encode the parameters.

问题很可能是,美国是非法的。逗号字符需要按百分比编码到q=London%2Cus。使用params属性对参数进行适当的编码。


Update

I tested it with my APPID (which I am not going to publish) and it worked.

我用APPID(我不打算发布)测试了它,结果成功了。

Here is my DEMO on PLNKR with the APPID removed.

这是我在PLNKR上的演示,并删除了APPID。


推荐阅读
author-avatar
viggieg-may_789
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有