我可以从我的网站向translate.google.com发送ajax请求吗?

时间:2014-08-24 11:38:39

标签: javascript jquery ajax

我想在我的网站上免费使用谷歌翻译,我可以这样做吗? 我怎么能这样做? 下面的代码说:

  

XMLHttpRequest cannot load http://translate.google.com/translate_a/single?client=t&sl=auto&tl=en&hl=en…l=0&tsel=3&q=%D9%86%D8%A7%D9%85+%DA%A9%D8%A7%D8%B1%D8%A8%D8%B1%DB%8C/f.txt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.或FireBug说:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://translate.google.com/translate_a/single?client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&oc=1&otf=2&ssel=0&tsel=3&q=%D9%86%D8%A7%D9%85+%DA%A9%D8%A7%D8%B1%D8%A8%D8%B1%DB%8C/f.txt. This can be fixed by moving the resource to the same domain or enabling CORS.

如何启用Cros?

  $.ajax({url:"http://translate.google.com/translate_a/single?client=t&sl=auto&tl=en&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&oc=1&otf=2&ssel=0&tsel=3&q=%D9%86%D8%A7%D9%85+%DA%A9%D8%A7%D8%B1%D8%A8%D8%B1%DB%8C/f.txt"
        ,type: 'GET'
        ,crossDomain: true
        ,dataType: "text"
        ,success:function(result){
            alert(result);
    }});

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

要与第三方来源(例如您的网站)进行谷歌翻译互动,请使用Google Translate API

API的费用约为每百万字符20美元,更多信息可在Pricing and Terms找到。

我可能误解了你的问题。如果您尝试翻译整个网站,可以使用免费的Google Website Translator

虽然付费是访问Google翻译的唯一方式,但如上所述,如果您需要免费的翻译API,请尝试使用Yandex翻译API。 希望这有帮助!