如何删除此错误“未捕获的TypeError:无法读取null的属性'translatedText'" [英] how to remove this error "Uncaught TypeError: Cannot read property 'translatedText' of null "

查看:92
本文介绍了如何删除此错误“未捕获的TypeError:无法读取null的属性'translatedText'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有完全相同的问题 如何使用jQuery通过Ajax使用Google Translate? 试图使用google api更改文本.但是我得到这个 未捕获的TypeError:无法读取null的属性"translatedText". 我正在尝试用其他语言(例如法语和德语)获得"hello world".您能告诉我我将如何实现这一目标吗?

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 ?

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

谢谢

推荐答案

您发送的请求出现错误:

The request you are sending is giving an error:

检查错误:尝试

$.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

这篇关于如何删除此错误“未捕获的TypeError:无法读取null的属性'translatedText'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆