Google URL shortener API - Uncaught TypeError: undefined is not a function
Google URL shortener API - Uncaught TypeError: undefined is not a function
我正在关注此站点的 google URL 更短 API 教程:
http://hayageek.com/google-url-shortener-api/
我正在跟进,这是我的代码:
<html>
<head>
</head>
<script type="text/javascript">
function makeShort()
{
var longURL=document.getElementByID("longurl").value; //error here
var request = gapi.client.urlshortener.url.insert({
'resource': {
'longUrl': longURL
}
});
request.execute(function(response)
{
if(response.id != null)
{
str = "<b>Long URL:</b>" +longURL+ "<br>";
str += "<b>Short URL:</b> <a href='"+response.id+ "'>"+response.id+"</a><br>";
document.getElementByID("output").innerHTML = str;
}
else
{
alert("error: creating short url n"+ response.error);
}
});
}
function getShortInfo()
{
var shortURL = document.getElementByID("shortURL").value;
var request = gapi.client.urlshortener.url.get({
'shortUrl':shortURL,
'projection':'FULL'
});
request.execute(function(response)
{
if(response.longURL!=null)
{
str ="<<b>Long URL</b>"+response.longURL+"<br>";
str += "<b>Create On:</b>"+response.created+"<br>";
str +="<b>Short URL Clicks:</b>"+response.analytics.allTime.shortUrlClicks+"<br>";
str +="<b>Long URL Clicks:</b>"+response.analytics.allTime.longUrlClicks+"<br>";
document.getElementByID("output").innerHTML = str;
}
else
{
alert("error: "+response.error);
}
});
}
function load()
{
gapi.client.setApiKey('APIKEYISHERE');
gapi.client.load('urlshortener', 'v1',function(){document.getElementById("output").innerHTML="";});
}
window.onload = load;
</script>
<script src="https://apis.google.com/js/client.js"></script>
<body>
URL: <input type="text" id="longurl" name="url"/> <br/>
<input type="button" value="Create Short URL" onclick="makeShort()" /> <br/> <br/>
URL: <input type="text" id="shorturl" name="url"/> <br/>
<input type="button" value="Get Short URL info" onclick="getShortInfo()"/>
<div id="output">Wait. Loading... </div>
</body>
</html>
但是,当我尝试 运行 URL 缩短器时,它在第 8 行给我一个 "Uncaught TypeError: undefined is not a function" 错误。
不确定我在这里做错了什么...我是初学者程序员。
我想通了,
getElementByID
应该是 getElementById
变化:
var longURL=document.getElementByID("longurl").value; //error here
收件人:
var longURL=document.getElementById("longurl").value; //Solved
变化:
var longURL=document.getElementByID("longurl").value; //error here
收件人:
var longURL=document.getElementById("longurl").value; //Solved
我正在关注此站点的 google URL 更短 API 教程:
http://hayageek.com/google-url-shortener-api/
我正在跟进,这是我的代码:
<html>
<head>
</head>
<script type="text/javascript">
function makeShort()
{
var longURL=document.getElementByID("longurl").value; //error here
var request = gapi.client.urlshortener.url.insert({
'resource': {
'longUrl': longURL
}
});
request.execute(function(response)
{
if(response.id != null)
{
str = "<b>Long URL:</b>" +longURL+ "<br>";
str += "<b>Short URL:</b> <a href='"+response.id+ "'>"+response.id+"</a><br>";
document.getElementByID("output").innerHTML = str;
}
else
{
alert("error: creating short url n"+ response.error);
}
});
}
function getShortInfo()
{
var shortURL = document.getElementByID("shortURL").value;
var request = gapi.client.urlshortener.url.get({
'shortUrl':shortURL,
'projection':'FULL'
});
request.execute(function(response)
{
if(response.longURL!=null)
{
str ="<<b>Long URL</b>"+response.longURL+"<br>";
str += "<b>Create On:</b>"+response.created+"<br>";
str +="<b>Short URL Clicks:</b>"+response.analytics.allTime.shortUrlClicks+"<br>";
str +="<b>Long URL Clicks:</b>"+response.analytics.allTime.longUrlClicks+"<br>";
document.getElementByID("output").innerHTML = str;
}
else
{
alert("error: "+response.error);
}
});
}
function load()
{
gapi.client.setApiKey('APIKEYISHERE');
gapi.client.load('urlshortener', 'v1',function(){document.getElementById("output").innerHTML="";});
}
window.onload = load;
</script>
<script src="https://apis.google.com/js/client.js"></script>
<body>
URL: <input type="text" id="longurl" name="url"/> <br/>
<input type="button" value="Create Short URL" onclick="makeShort()" /> <br/> <br/>
URL: <input type="text" id="shorturl" name="url"/> <br/>
<input type="button" value="Get Short URL info" onclick="getShortInfo()"/>
<div id="output">Wait. Loading... </div>
</body>
</html>
但是,当我尝试 运行 URL 缩短器时,它在第 8 行给我一个 "Uncaught TypeError: undefined is not a function" 错误。
不确定我在这里做错了什么...我是初学者程序员。
我想通了,
getElementByID
应该是 getElementById
变化:
var longURL=document.getElementByID("longurl").value; //error here
收件人:
var longURL=document.getElementById("longurl").value; //Solved
变化:
var longURL=document.getElementByID("longurl").value; //error here
收件人:
var longURL=document.getElementById("longurl").value; //Solved