我有完全相同的问题 How can I use Google Translate via Ajax using jQuery? 尝试使用 google api 更改文本。但我得到了这个 未捕获的类型错误:无法读取 null 的属性“translatedText”。 我正在尝试用不同的语言(例如法语和德语)获得“hello world”。您能告诉我如何实现这一目标吗?
$.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);
}
});
谢谢
最佳答案
您发送的请求出现错误:
检查错误:尝试
$.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);
}
});
使用替代代码
$.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);
}
});
详细文档 API DOC
关于javascript - 如何删除此错误 "Uncaught TypeError: Cannot read property ' translatedText' of null ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470486/