簡體   English   中英

Google URL Shortener API-未捕獲的TypeError:undefined不是函數

[英]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行給了我一個“未捕獲的TypeError:未定義不是函數”錯誤。

不知道我在做什么錯...我是初學者。

變更:

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

至 :

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

我想到了,

getElementByID應該是getElementById

變更:

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

至 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM