从Outlook邮件加载项调用Web服务

时间:2015-09-30 09:01:52

标签: jquery ajax rest office-addins office-js

我对Outlook Mail Add-In的这个问题有点困惑。 我正在使用Office 365开发人员帐户,其中将显示Mail Add-In(在线,在浏览器中): enter image description here

单击按钮时,我想调用REST Web服务。

首先,让我发布我的HTML:

<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title></title>
    <script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>

    <link href="../../Content/Office.css" rel="stylesheet" type="text/css" />
    <script src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js" type="text/javascript"></script>

    <!-- To enable offline debugging using a local reference to Office.js, use:                        -->
    <!-- <script src="../../Scripts/Office/MicrosoftAjax.js" type="text/javascript"></script>  -->
    <!-- <script src="../../Scripts/Office/1.1/office.js" type="text/javascript"></script>  -->

    <link href="../App.css" rel="stylesheet" type="text/css" />
    <script src="../App.js" type="text/javascript"></script>

    <link href="Home.css" rel="stylesheet" type="text/css" />
    <script src="Home.js" type="text/javascript"></script>
</head>
<body>
    <!--Header and Footer tags not supported-->
    <div id="content-main">
        Click this button to test the create method
        <br />
        <label id="lblMessage">Message will appear here</label>
        <button id="btnTest" type="button">Perform Test Create</button>

        <!--<iframe id="my_api_iframe"></iframe>-->
    </div>
</body>
</html>

这是我们在第一个屏幕截图中看到的HTML。

Office.initialize JS代码,适用于那些想要查看的人:

Office.initialize = function (reason) {
        //_mailbox = Office.context.mailbox;
        ////Request identity token from Exchange Server.
        //_mailbox.getUserIdentityTokenAsync(getTokenCallback);

        $(document).ready(function () {
            app.initialize();
            //SET Variables
            loggedInUserDisplayName = Office.context.mailbox.userProfile.displayName;
            loggedInUserEmailAddress = Office.context.mailbox.userProfile.emailAddress;
            var conversationID = Office.context.mailbox.item.conversationId;
            var internetMessageID = Office.context.mailbox.item.internetMessageId;
            var itemID = Office.context.mailbox.item.itemId;

            //$('#lblMessage').text("ConversationID:" + convoID + " InternetMessageID:" + intMessID + " ItemID:" + itemID);

            $("#btnTest").click(function () {
                GetList();
                //GetList2();
            });
        });
    };

我尝试过不同的方法。单击按钮时,它会调用指定的函数,这是我尝试过的几种方法:

function GetList() {
        jQuery.support.cors = true;
        $.ajax({
            type: "GET",
            url: [URL to WebMethod],
            success: function (data, textStatus) {
                $('#lblMessage').text("Success");
            },
            error: function (xhr, textStatus, errorThrown) {
                $('#lblMessage').text("Error: " + errorThrown + " StatusCode: " + textStatus + " XHR: " + xhr.readyState);
            }
        });
    }

但是当单击按钮并调用此函数时,这是我得到的错误: enter image description here

我试过的第二个功能:

function GetList2() {
        $.ajax({
            type: "GET",
            url: [URL to WebMethod], xhr: function () {
                return new ($('#my_api_iframe')[0].contentWindow.XMLHttpRequest)();
            }, success: function (html) {
                // format and output result
                $('#lblMessage').text(html);
            }, error: function (xhr, textStatus, errorThrown) {
                // format and output result
                $('#lblMessage').text(errorThrown);
            }
        });
    }

但无济于事。这是我得到的错误: enter image description here

我尝试的最后一项功能:

function getTokenCallback(asyncResult) {
        var token = asyncResult.value;

        // Create a web service call and pass the token as part of the call.
        _xhr = new XMLHttpRequest();
        _xhr.open("GET", [URL to WebService]);
        _xhr.setRequestHeader("Content-Type", "application/xml; charset=utf-8");
        _xhr.onreadystatechange = readyStateChange;

        var request = new Object();
        request.token = token;
        request.
        request.phoneNumbers = _om.get_item().getEntities().phoneNumbers;

        _xhr.send(JSON.stringify(request));
    }

但我得到同样的#34;访问被拒绝&#34;错误与之前一样,在我执行_xhr.open()的时候。

任何人都可以帮助我找到正确的方向来调用这些REST服务吗?它们都以XML格式返回数据,这就是我使用&#34; application / xml&#34;的内容类型的原因。

任何帮助都将受到高度赞赏,即使它与我尝试的方式完全不同:)。

1 个答案:

答案 0 :(得分:0)

您的iframe HTML元素已被注释掉。尝试取消注释。至少这就是GetList2()失败的原因。

所以改变

<!--<iframe id="my_api_iframe"></iframe>-->

<iframe id="my_api_iframe"></iframe>