【问题标题】:Translate Texts using ajax and google translate API使用 ajax 和谷歌翻译 API 翻译文本
【发布时间】:2019-05-20 12:03:09
【问题描述】:

当我单击 html 中的按钮时,我想执行此函数并进行 ajax 调用,但似乎 API Url 已被弃用,因为我的控制台中出现错误:未捕获的 TypeError:无法读取属性 'translatedText'为空

有什么想法吗?谢谢

这是我的 javascript 函数:

function translateCaption() {
$.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(result.responseData.translatedText);
    },
    error: function (XMLHttpRequest, errorMsg, errorThrown) {
        alert(errorMsg);
    }
});
}

这是我的html:

<button onclick="translateCaption()" class="button-translate" type="button">Translate</button>

【问题讨论】:

    标签: ajax api google-translate


    【解决方案1】:

    目前仅使用前端解决方案无法解决。 您的代码的响应(可以在浏览器的请求响应中看到)是: /* 回调 */jQuery22108465697200462694_1552511749760({"responseData": null, "responseDetails": "请使用 Translate v2。请参阅 http://code.google.com/apis/language/translate/overview.html", "responseStatus":第403章)

    我建议查看https://cloud.google.com/translate/docs/translating-text 提供不同编程语言(用于后端)的示例,并参考 GitHub。

    【讨论】:

      猜你喜欢
      • 2016-11-18
      • 2011-12-26
      • 1970-01-01
      • 2011-06-06
      • 2017-03-13
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多