繁体   English   中英

如何删除此错误“未捕获的TypeError:无法读取null的属性'translatedText'”

[英]how to remove this error “Uncaught TypeError: Cannot read property 'translatedText' of null ”

我有完全相同的问题如何使用jQuery通过Ajax使用Google Translate? 尝试使用google api更改文本。但是我收到此Uncaught TypeError:无法读取null的属性'translatedText'。 我正在尝试用其他语言(例如法语和德语)获得“ hello world”。您能告诉我如何实现吗?

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

谢谢

您发送的请求出现错误:

检查错误:尝试

$.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的详细文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM