无法弄清楚如何正确使用API​​

时间:2016-09-29 17:18:08

标签: javascript html api http get

所以我想创建一个向当地国会议员发送信件的网站。该网站主要包含一个表单,点击"提交,"应使用Google思域信息API返回代表联系人。我是HTTP通信的新手,所以我不确定为什么在尝试使用GET时,我收到了403错误。

我甚至不知道我是否正确使用了API。这里的目标是提交一个地址(以及其他信息),将该数据发送到API,并让它根据用户提交的信息返回一个国会议员。

Google Civic Info API链接:https://developers.google.com/civic-information/docs/v2/representatives/representativeInfoByAddress

我的代码:

<!DOCTYPE html>

<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content = "IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Lob Application</title>

<link href = "stylesheet.css" rel = "stylesheet" >
</head>

<body>
 <div id = formInput class = "container-fluid">
 <form name = "submitDataForm" method = "GET" onsubmit="getData()">
  Name:<br>
    <input type = "text" name = "name"><br>
  Address Line 1:<br>
    <input type = "text" name = "addressLineOne"><br>
  Address Line 2:<br>
    <input type = "text" name = "addressLineTwo"><br>
  City:<br>
    <input type = "text" name = "city"><br>
  State:<br>
    <input type = "text" name = "state"><br>
  Zip Code:<br>
    <input type = "text" name = "zip"><br>
  Message:<br>
    <input type = "text" name = "message"/><br>
  <input id = "submit" type = "submit" value = "Submit"/>
</form>
</div>
<script src="processData.js" type = "text/javascript"></script>
</body>
</html>

Javascript:

function getData(){
var xhr = new XMLHttpRequest()
xhr.open("GET", "https://www.googleapis.com/civicinfo/v2/representatives/", false);
xhr.send(null);
console.log(xhr.status);
console.log(xhr.statusText);
}

1 个答案:

答案 0 :(得分:0)

您似乎已达到API使用的每日配额限制。您必须通过Google Developers Console注册并获取有效的API_KEY。

请查看this以了解您的错误消息,并this page了解如何使用API​​