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