2016-09-29 48 views
0

所以我想创建一个网站,发送一封信给当地的国会议员。该网站主要由一个表单组成,在点击“提交”后,应该让代表联系使用Google Civic Info API的代表。我对HTTP通信很陌生,所以我不确定为什么,当试图使用GET时,我得到了403错误。找不到如何正确使用API​​

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

链接到谷歌公民信息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); 
} 

回答

0

看来你达到使用API​​的每日配额限制。您必须通过Google Developers Console进行注册并获取有效的API_KEY。

看看this理解你的错误信息和对this page理解如何使用API​​