【发布时间】:2021-03-29 20:42:17
【问题描述】:
我正在使用一个简单的 Web API 来获取城市 伦敦 的天气详细信息。但是,我没有得到所需的响应,而是 401 错误。我做错什么了吗?
HTML
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('button').click(function(){
$.ajax({
type: "get",
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
dataType: "json",
contentType: 'application/json',
success: function(data) {
//What to do on success
}
});
});
});
</script>
</head>
<body>
<button>Click Me</button>
</body>
</html>
使用以下 api
http://openweathermap.org/current
错误
【问题讨论】:
-
调用该 URL 我得到以下响应:
{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}您是否在请求中传递了有效的 API 密钥? openweathermap.org/faq#error401 -
请参阅所附页面和文档。我只从那里选择了网址。
-
文档声明您需要有一个 API 密钥才能使用它:openweathermap.org/appid。另请注意,在免费帐户上,您调用 API 的次数受到限制。如果您的网站需要依赖此服务,我建议您支付 SLA
-
是的,我现在明白了。我拿到了钥匙……现在再试一次。
-
我有一个钥匙说:12345。现在你能告诉如何通过它吗?
标签: jquery web-services rest asp.net-web-api