我在使用 Outlook 邮件加载项时遇到的这个问题有点困扰。我正在使用 Office 365 开发人员帐户,其中将显示邮件加载项(在线,在浏览器中):
单击按钮时,我想调用 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);
}
});
}
我尝试的第二个功能:
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);
}
});
}
我尝试的最后一个功能:
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));
}
但是在执行_xhr.open() 时,我得到了与以前相同的“访问被拒绝”错误。
任何人都可以帮助我在正确的方向上调用这些 REST 服务吗?它们都以 XML 格式返回数据,这就是我使用“application/xml”的 ContentType 的原因。
任何帮助将不胜感激,即使它与我正在尝试做的方式完全不同:)。