我正在构建一个 JS 加载项,它只需在单击功能区中的按钮时将带有添加注释“STOP”的消息转发到定义的地址。为了转发,我通过请求调用 REST API POST。
event.completed()在函数末尾添加(event)作为参数时,getCallbackTokenAsync 根本不运行。
相反,如果没有它,加载项会正确转发消息,但信息栏不会消失并且脚本会继续循环运行:
表示脚本正在运行的信息栏:https ://i.stack.imgur.com/zGa2K.png
event.completed()关于调用 REST API 时我应该如何正确处理的任何想法?
// the function sendAsStops is called directly from the manifest file
function sendAsStops(event) {
console.log("Initialising STOP command.");
var restHost = Office.context.mailbox.restUrl;
var itemId = getItemRestId();
console.log(itemId);
Office.context.mailbox.getCallbackTokenAsync({ isRest: true }, function (result) {
if (result.status === Office.AsyncResultStatus.Succeeded) {
console.log('Sending email...');
var accessToken = result.value;
console.log(result);
var getMessageUrl = restHost + '/v2.0/me/messages/' + itemId + '/forward';
$.ajax({
url: getMessageUrl,
type: 'post',
headers: { 'Authorization': 'Bearer ' + accessToken },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
'Comment': 'STOP',
'ToRecipients': [{'EmailAddress': { 'Address': 'name@address.com' }}],
})
}).done(function() {
console.log("Stop successfully forwarded.");
}).fail(function(error) {
console.log("Failed to send");
});
} else {
console.log("Unable to proceed. Ref: " + result.status);
return false;
}
});
event.completed();
}
function getItemRestId() {
console.log("Getting item ID...");
if (Office.context.mailbox.diagnostics.hostName === 'OutlookIOS') {
console.log("ID ready to use.");
return Office.context.mailbox.item.itemId;
} else {
console.log('Converting ID...');
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}
www说
相关分类