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,