简体   繁体   English

Google URL Shortener API-未捕获的TypeError:undefined不是函数

[英]Google URL shortener API - Uncaught TypeError: undefined is not a function

I am following the google URL shortner API tutorial from this site: 我正在从该站点关注google URL缩短API教程:

http://hayageek.com/google-url-shortener-api/ http://hayageek.com/google-url-shortener-api/

I am following along and this is my code: 我一直在关注,这是我的代码:

<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>

However, when I try to run the URL shortener, it gives me an "Uncaught TypeError: undefined is not a function" error on line 8. 但是,当我尝试运行URL缩短程序时,它在第8行给了我一个“未捕获的TypeError:未定义不是函数”错误。

Not sure what I'm doing wrong here... I am a beginner programmer. 不知道我在做什么错...我是初学者。

Change : 变更:

var longURL=document.getElementByID("longurl").value; //error here

To : 至 :

var longURL=document.getElementById("longurl").value; //Solved

I Figured it out, 我想到了,

getElementByID should be getElementById getElementByID应该是getElementById

Change : 变更:

var longURL=document.getElementByID("longurl").value; //error here

To : 至 :

var longURL=document.getElementById("longurl").value; //Solved

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM