简体   繁体   中英

Calling Web Service From Outlook Mail Add-In

I'm a bit stuck on this one problem I have with an Outlook Mail Add-In. I am using an Office 365 developer account, where the Mail Add-In will show (Online, in browser): 在此处输入图片说明

I want to call a REST Web Service when I click the button.

First, let me post my 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>

This is the HTML for what we see in the first screenshot.

The Office.initialize JS code, for those who want to see:

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();
            });
        });
    };

I have tried different ways to do this. When the button is clicked, it calls the specified function, here's a few ways I have tried:

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);
            }
        });
    }

But when the button is clicked and I call this function, this is the error I get: 在此处输入图片说明

The 2nd function I tried:

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);
            }
        });
    }

But no avail. This is the error I get: 在此处输入图片说明

Last function I tried:

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));
    }

But I get the same "access is denied" error as before, at the point where I do the _xhr.open().

Can anyone maybe help me in the right direction to get these REST Services called? They all return data in XML Format, that's why I used ContentType of "application/xml".

Any help will be greatly appreciated, even if it's a completely different way than what I'm trying to do :).

Your iframe HTML element is commented out. Try to uncomment it. At least this is why the GetList2() fails.

So change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM