我想在Javascript中建立一个简单的网站,当加载时,打印"hello"中文
为此,我尝试使用谷歌翻译呼叫,如下所示:
<!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=translate 403
似乎谷歌服务器拒绝了我的电话。谁能帮我解决这个问题?
对
出于安全原因,现代浏览器不允许跨域访问。看到歌珥。
这意味着发出请求的网页和它试图加载的网页必须位于同一台服务器上。
你必须使用谷歌的云翻译API。