Reputation: 655
I have exactly same Question How can I use Google Translate via Ajax using jQuery? trying to change text using google api .But I am getting this Uncaught TypeError: Cannot read property 'translatedText' of null. I am trying to get "hello world" in different language (like french and german).can you please tell me how I will achieve this ?
$.ajax({
url: 'https://ajax.googleapis.com/ajax/services/language/translate',
dataType: 'jsonp',
data: { q: 'Hello world!', // text to translate
v: '1.0',
langpair: 'en|es' }, // '|es' for auto-detect
success: function(result) {
alert('dd');
console.log(result)
alert(result.responseData.translatedText);
},
error: function(XMLHttpRequest, errorMsg, errorThrown) {
alert('dddd')
alert(errorMsg);
}
});
Thanks
Upvotes: 0
Views: 3455
Reputation: 6228
The request you are sending is giving an error:
to check error : try
$.ajax({
url: 'https://ajax.googleapis.com/ajax/services/language/translate',
dataType: 'jsonp',
data: { q: 'Hello world!', // text to translate
v: '1.0',
langpair: 'en|es' }, // '|es' for auto-detect
success: function(result) {
alert(JSON.stringify(result));
//console.log(result);
alert(result.responseData.translatedText);
},
error: function(XMLHttpRequest, errorMsg, errorThrown) {
alert('dddd');
alert(errorMsg);
}
});
Code using alternative
$.ajax({
url: 'http://api.mymemory.translated.net/get',
data: { q: 'Hello world!', // text to translate
langpair: 'en|es' }, // '|es' for auto-detect
success: function(result) {
//console.log(result);
alert(result.responseData.translatedText);
},
error: function(XMLHttpRequest, errorMsg, errorThrown) {
alert('dddd');
alert(errorMsg);
}
});
Detailed Document on API DOC
Upvotes: 1