未捕获类型错误:不能读取属性'translatedText'null "

how to remove this error "Uncaught TypeError: Cannot read property 'translatedText' of null "

本文关键字:属性 translatedText quot null 读取 不能读 类型 错误 不能      更新时间:2023-09-26

我也有同样的问题我如何使用谷歌翻译通过Ajax使用jQuery?试图改变文本使用谷歌api。但我得到这个未捕获的类型错误:无法读取null属性'translatedText'。我试图得到"你好世界"在不同的语言(如法语和德语)。你能告诉我怎么做吗?

http://jsfiddle.net/n0217sn0/

$.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);
    }  
});

谢谢

您正在发送的请求给出错误:

检查错误: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);
    }  
});

使用替代

的代码
$.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文档