【问题标题】:Trying to call google translator api from Javascript code, getting 403 error尝试从 Javascript 代码调用谷歌翻译器 api,得到 403 错误
【发布时间】:2021-01-23 08:34:10
【问题描述】:

我想用 Javascript 构建一个简单的网站,加载时会用中文打印“你好”。

为此,我尝试使用谷歌翻译调用,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- bootstrap.min.css + some js related files -->

<body onload="myFunction()">
</body>
    </head>
    
    <script>
    
        function myFunction()
        {
            //alert("hello");

            var ourRequest = new XMLHttpRequest();
        var API;
        API = 'https://translate.google.co.in/?sl=auto&tl=zh-CN&text=hello&op=translate';   
        
        //alert(API);
        ourRequest.onreadystatechange = function() 
            {
                    if (this.readyState == 4 && this.status == 200)
                         {
alert(ourRequest.responseText);                     
                        }
            };
        ourRequest.open('GET', API,false);
        ourRequest.send();
            
            
        }
    
    </script>  
    
    
</html>
    

当我尝试运行它时,我收到以下错误:

try.html:43 GET https://translate.google.co.in/?sl=auto&tl=zh-CN&text=hello&op=translate403

似乎谷歌服务器拒绝了我的电话。 有人可以帮我解决这个问题吗?

问候

【问题讨论】:

    标签: javascript http-status-code-403


    【解决方案1】:

    出于安全原因,现代浏览器不允许跨域访问。见CORS

    这意味着发出请求的网页和它尝试加载的网页都必须位于同一台服务器上。

    您必须使用Google's Cloud Translation API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2015-04-04
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多