我正在做一个小项目,其中有两个div,一个用于English
,另一个用于Arabic
。我已经创建了这个项目,但问题是我没有从English to Arabic
获得翻译,下面是代码我试过了。我不知道我做错了什么。如有任何帮助,不胜感激。
编辑:我的请求成功发送到谷歌,但每次错误体都在ajax中执行
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script src="~/Scripts/jquery-2.1.4.js"></script>
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/jquery.blockUI.js"></script>
</head>
<body>
<div id="sourceText">Hello world</div>
<div id="translation"></div>
<script>
var sourceText = escape(document.getElementById("sourceText").innerHTML);
var source = "https://www.googleapis.com/language/translate/v2?key=AIzaSyBBsdXcsCLjSex4-9EReA_MqsBFrLrkwm4&source=en&target=ar&callback=translateText&q=";
$.ajax({
url: source + encodeURIComponent(sourceText),
dataType: 'jsonp',
async: false,
success: function (data) {
alert(data.data.translations[0].translatedText);
$('#translation').html(data.data.translations[0].translatedText);
},
error: function(x, e) {
alert('Error occured while translating the text');
}
});
</script>
</body>
</html>
您没有定义回调函数:
var sourceText = escape(document.getElementById("sourceText").innerHTML);
var source = "https://www.googleapis.com/language/translate/v2?key=AIzaSyBBsdXcsCLjSex4-9EReA_MqsBFrLrkwm4&source=en&target=ar&callback=translateText&q=";
$.ajax({
url: source + encodeURIComponent(sourceText),
dataType: 'jsonp',
jsonpCallback: "translateText",
async: false,
error: function(x, e) {
alert('Error occured while translating the text');
}
});
function translateText(data){
alert();
}
这里是一个jsfiddle:jsfiddle