2012-07-26 74 views
1

我试图将MS Bing Translator的当前版本转换为新的Azure版本。Microsoft Azure Translator AJAX API不能正常工作

我创建接入令牌作为新的文档中,虽然由Microsoft提供下面的例子(为天青)所述正常工作:

function translate() { 

    var from = "en", to = "es", text = "hello world"; 
    var s = document.createElement("script"); 
    s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" + 
      "?appId=" + settings.appID + 
      "&from=" + encodeURIComponent(from) + 
      "&to=" + encodeURIComponent(to) + 
      "&text=" + encodeURIComponent(text) + 
      "&oncomplete=mycallback"; 
    document.body.appendChild(s); 
} 

function mycallback(response) { 
    alert(response); 
} 

我想上述代码转换为一个jQuery呼叫。

我修改从工作的以前版本的类似jQuery的AJAX调用,但parseerror-jQuery17206897480448242277_1343343577741 was not called发出:

function jqueryTranslate() { 
    var p = {}; 
    p.appid = settings.appID; 
    p.to = "es"; 
    p.from = "en"; 
    p.text = "Goodbye Cruel World"; 
    p.contentType = 'text/html'; 
    $.ajax({ 
     url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate', 
     data: p, 
     dataType: 'jsonp', 
     jsonp: 'oncomplete', 
     complete: function (request, status) { 
     }, 
     success: function (result, status) { 
     alert(result); 
     }, 
     error: function (a, b, c) { 
     alert(b + '-' + c); 
     } 
    }); 
    } 

我非常欣赏和什么错误,所以TIA你的时间的理解。

回答

1

另一个问题是Bing AppID机制用于验证转换器已被弃用。

微软有一个博客帖子,详细说明获得访问翻译在Windows Azure市场这里的过程:

http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx

有ASP.NET中的例子在这里: http://blogs.msdn.com/b/translation/p/gettingstarted2.aspx

推荐(至少)是把你的代码放到ASP.NET,PHP,Node或类似的东西中,这样你的客户端ID和客户端秘密就不会暴露。

获取访问令牌后,需要将其写入服务调用的HTTP头中。 ASP.NET示例显示,它适应JQuery应该相对容易。

+0

我没有在C#中实现新的访问令牌机制。你提供的第二个链接似乎是一个值得追求的策略。谢谢! – 2012-07-27 16:00:05

0

您可以尝试在您的呼叫中添加jsonpCallback并为其定义新的功能。当我比较你的jQuery代码和来自微软的例子时,这似乎是缺少的。

function jqueryTranslate() { 
    var p = {}; 
    p.appid = settings.appID; 
    p.to = "es"; 
    p.from = "en"; 
    p.text = "Goodbye Cruel World"; 
    p.contentType = 'text/html'; 
    $.ajax({ 
     url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate', 
     data: p, 
     dataType: 'jsonp', 
     jsonp: 'oncomplete', 
     jsonpCallback: 'onCompleteCallback', <------------------ THIS LINE 
     complete: function (request, status) { 
     }, 
     success: function (result, status) { 
     alert(result); 
     }, 
     error: function (a, b, c) { 
     alert(b + '-' + c); 
     } 
    }); 
    } 

    function onCompleteCallback(response) { <------------------- THIS FUNCTION 
    alert('callback!'); 
    } 
4

要将Bing翻译器与认证令牌一起使用,首先需要一个服务器端脚本,如此PHP脚本token.php。它将从您的网页上的JavaScript每隔9分钟被调用一次。

<?php 
$ClientID="your client id"; 
$ClientSecret="your client secret"; 

$ClientSecret = urlencode ($ClientSecret); 
$ClientID = urlencode($ClientID); 

// Get a 10-minute access token for Microsoft Translator API. 
$url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; 
$postParams = "grant_type=client_credentials&client_id=$ClientID&client_secret=$ClientSecret&scope=http://api.microsofttranslator.com"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$rsp = curl_exec($ch); 

print $rsp; 
?> 

然后,这个html页面将显示一个从英文翻译成法文的双盒界面。

注意:此文章的早期版本缺少前5行,因此无法加载jQuery。 (很抱歉,@ DB1)工作脚本是在这里在线:

http://www.johndimm.com/bingtrans/

<html> 

<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

    <script language="javascript"> 
    var g_token = ''; 

    function onLoad() { 
     // Get an access token now. Good for 10 minutes. 
     getToken(); 
     // Get a new one every 9 minutes. 
     setInterval(getToken, 9 * 60 * 1000); 
    } 

    function getToken() { 
     var requestStr = "/bingtrans/token.php"; 

     $.ajax({ 
     url: requestStr, 
     type: "GET", 
     cache: true, 
     dataType: 'json', 
     success: function (data) { 
      g_token = data.access_token; 
     } 
     }); 
    } 

    function translate(text, from, to) { 
     var p = new Object; 
     p.text = text; 
     p.from = from; 
     p.to = to; 
     p.oncomplete = 'ajaxTranslateCallback'; // <-- a major puzzle solved. Who would have guessed you register the jsonp callback as oncomplete? 
     p.appId = "Bearer " + g_token; // <-- another major puzzle. Instead of using the header, we stuff the token into the deprecated appId. 
     var requestStr = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate"; 

     window.ajaxTranslateCallback = function (response) { 
     // Display translated text in the right textarea. 
     $("#target").text(response); 
     } 

     $.ajax({ 
     url: requestStr, 
     type: "GET", 
     data: p, 
     dataType: 'jsonp', 
     cache: true 
     }); 
    } 


    function translateSourceTarget() { 
     // Translate the text typed by the user into the left textarea. 
     var src = $("#source").val(); 
     translate(src, "en", "fr"); 
    } 
    </script> 
    <style> 
    #source, 
    #target { 
     float: left; 
     width: 400px; 
     height: 50px; 
     padding: 10px; 
     margin: 10px; 
     border: 1px solid black; 
    } 
    #translateButton { 
     float: left; 
     margin: 10px; 
     height: 50px; 
    } 
    </style> 
</head> 

<body onload="onLoad();"> 

    <textarea id="source">Text typed here will be translated.</textarea> 
    <button id="translateButton" onclick="translateSourceTarget();">Translate English to French</button> 
    <textarea id="target"></textarea> 

</body> 

</html>