none
How can i access bing translate api for office 2013 with task pane app by javascript? RRS feed

  • Question

  • I want to use bing translate api in my office app,then i wrote the following code,but Access denied...Need to configure the remote access api?

    And that code run at browser no problem

    Please help... 

    Office.initialize = function (reason) {
            $(document).ready(function () {
                app.initialize();
                onTranslate();
            });
        };

        function onTranslate() {
            var src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=getSuccess&appId=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C&from=en&to=zh-cn&text=hello";
            $.ajax({
                url: src,
                type:"GET",
                success: getSuccess,
                dataType: "JSONP",
                error:errorFunc
            });
        };
        function getSuccess(result){
            alert(result);
        }

        function errorFunc(a,b,c) {
            app.showNotification(b);
        }

    • Edited by XLYang Monday, March 10, 2014 6:31 AM
    Monday, March 10, 2014 2:55 AM

Answers

  • Hi Yang,

    The Bing Translation API you called should work for both browser and Office app. The simple code section I used to verify it:

    Office.initialize = function (reason) {
        $(document).ready(function () {
            translate("hello");
        });
    };

    function translate(text) {
        var txt = "text=" + text;
        var lang = "&to=zh-cn";
        var appId = "&appid=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C";
        var func = "&oncomplete=translated";

        $("<script><\/script>")
                    .attr("src", "http://api.microsofttranslator.com/V2/ajax.svc/Translate?" + txt + lang + appId + func)
                    .appendTo("HEAD");
    }

    function translated(result) {
        document.getElementById("selectedDataTxt").innerText = result;
    }

    If you are working with Visual Studio, suggest to set several breakpoints in your code to see which part goes wrong, such as maybe it doesn't get a given element.

    Thanks

    Leon

    • Marked as answer by XLYang Tuesday, March 11, 2014 3:06 AM
    Monday, March 10, 2014 9:22 AM

All replies

  • Hi Yang,

    The Bing Translation API you called should work for both browser and Office app. The simple code section I used to verify it:

    Office.initialize = function (reason) {
        $(document).ready(function () {
            translate("hello");
        });
    };

    function translate(text) {
        var txt = "text=" + text;
        var lang = "&to=zh-cn";
        var appId = "&appid=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C";
        var func = "&oncomplete=translated";

        $("<script><\/script>")
                    .attr("src", "http://api.microsofttranslator.com/V2/ajax.svc/Translate?" + txt + lang + appId + func)
                    .appendTo("HEAD");
    }

    function translated(result) {
        document.getElementById("selectedDataTxt").innerText = result;
    }

    If you are working with Visual Studio, suggest to set several breakpoints in your code to see which part goes wrong, such as maybe it doesn't get a given element.

    Thanks

    Leon

    • Marked as answer by XLYang Tuesday, March 11, 2014 3:06 AM
    Monday, March 10, 2014 9:22 AM
  • Hi Leon

    Thanks for your reply,i found the mistake,is the security mechanism of web browser stopped.When errors occur,i click the continue button,and then back to word,App showed that the web browser to prevent unsafe content,then I manually select show all content,finally it worked....

    How to avoid the security level in the code?

    Thanks

    Monday, March 10, 2014 3:47 PM
  • Could you show me the link you tested, cause the Bing service above won't trigger any security warning on my machine?
    Tuesday, March 11, 2014 6:41 AM