TypeError: Cannot read property 'translations' of undefined

1,239 views
Skip to first unread message

Alex Trần

unread,
Aug 13, 2013, 4:00:47 AM8/13/13
to google-tra...@googlegroups.com

I use the following code from this Google Translate API's Get Started

  <head>
    <title>Translate API Example</title>
  </head>
<body>
  <div id="sourceText">Hello world</div>
  <div id="translation"></div>
  <script>
  function translateText(response) {
    // ERROR SHOW HERE
    document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
  }
  </script>
  <script>
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    var sourceText = escape(document.getElementById("sourceText").innerHTML);

    var source = 'https://www.googleapis.com/language/translate/v2?key=MY-KEY&source=en&target=de&callback=translateText&q=' + sourceText;
    newScript.src = source;

    // When we add this script to the head, the request is sent off.
    document.getElementsByTagName('head')[0].appendChild(newScript);
  </script>
</body>

And I get the error "TypeError: Cannot read property 'translations' of undefined"

Please tell me how to fix this error,

Thanks,

Alex Trần

unread,
Aug 15, 2013, 7:55:27 PM8/15/13
to google-tra...@googlegroups.com
Issue was fixed when I add "console.log(response)" in to THE ERROR SHOW HERE,
Thanks,
Reply all
Reply to author
Forward
0 new messages