无法从 Outlook OnMessageSend 智能警报加载项发送 XMLHttpRequest

问题描述 投票:0回答:1

我正在开发一个 Outlook 加载项,其中包含 OnMessageSend 智能警报事件处理程序。在 Outlook Web App 中,一切都按照我的预期运行,但我尝试从发送 Outlook 事件处理程序发送的任何 XMLHttpRequest 在桌面应用程序中都会失败。不存在跨域问题,因为我将请求发送到脚本所在的同一域(测试中的 localhost:44300)。

发送处理程序看起来像这样:

function onMessageSendHandler(outlookE) {
    Office.context.mailbox.item.sessionData.getAsync("stuffFromTaskPane", (e) => {
        var postObj = e.value;
         if (postObj) {
            Office.context.mailbox.item.subject.getAsync(sbj => {
                postObj.Subject = sbj.value;
                Office.context.mailbox.item.body.getAsync("html", bdy => {
                    postObj.Body = bdy.value;
                    var req = new XMLHttpRequest();
                    req.onload = () => {
                        //test code
                        outlookE.completed({ allowEvent: false, errorMessage: "request loaded" });
                    };
                    req.onerror = () => {
                        outlookE.completed({ allowEvent: false, errorMessage: JSON.stringify(req) });
                    };
                    req.onabort = () => {
                        outlookE.completed({ allowEvent: false, errorMessage: "Request to Server Was Aborted." });
                    };

                    //Real request commendted out 
                    //req.open("POST", "/someApiPath");
                    //req.setRequestHeader("Content-Type", "application/json");
                    //req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                    //req.send(JSON.stringify(postObj));
                    
                    //Test request just to try and get something
                    req.open("GET", "/SomeJsPath.js");
                    req.send();
                });
            });
         }
         else {
            outlookE.completed({ allowEvent: true });
         }
    });
}

// ------------------------------------- linkEvent ------------------------------------- \\
function linkEvent() {
    //When the webapp first calls this Office.context is not defined so we skip the first call in the webapp, but when this is called onReady everything is good to go
    if (Office.context && (Office.context.platform === Office.PlatformType.PC || Office.context.platform == null)) {
        Office.actions.associate("onMessageSendHandler", onMessageSendHandler);
    }
}

//The web-app needs to do the linkEvent at onReady. The desktop app just calls it.
if (Office.onReady) {
    Office.onReady(linkEvent);
}
linkEvent();

错误回调最终被调用,并给我一个如下所示的 req 对象:

{
  "UNSENT": 0,
  "OPENED": 1,
  "HEADERS_RECEIVED": 2,
  "LOADING": 3,
  "DONE": 4,
  "readyState": 4,
  "status": 0,
  "timeout": 0,
  "withCredentials": true,
  "upload": {},
  "_aborted": false,
  "_hasError": true,
  "_method": "GET",
  "_perfKey": "network_XMLHttpRequest_/SomeJsPath.js",
  "_response": "Error code = 15 \n",
  "_ur1": "/SomeJsPath.js",
  "_timedOunfalseLtrackingName": "unknown",
  "_incrementalEvents": false,
  "_performanceLogger": {
    "timespans": {
      "network XMLHttpRequest_/SomeJsPath.js": {
        "startTime": 17021415053926
      }
    },
    "_extras": {},
    "_points": {
      "initializeCore_start": 1704415053788,
      "initializeCore_end": 1704415053795
    },
    "_pointExtras": {},
    "_closed": false
  },
  "_requestId": null,
  "_headers": {},
  "_responseType": "",
  "_sent": true,
  "_lowerCaseResponseHeaders": {},
  "_subscriptions": []
}

我猜测“_hasError”:true和“_response”:“错误代码= 15 “这些行试图告诉我一些事情,但我不确定什么。如果我尝试更改请求的 URL 以使用 GET 请求请求某个任意站点,我会在 req 对象上收到 CORS 错误,抱怨来源(我加载项发送事件确实在桌面应用程序中正常激活,只是 HTTP 请求不起作用。

我一直在关注本教程:https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/smart-alerts-onmessagesend-walkthrough?tabs=xmlmanifest

这是桌面 Outlook 应用程序。我听说有些人已经可以选择升级到“新”外观的 Outlook 应用程序,我认为它更像是网络应用程序,但我还没有。

关于如何从 on-send 事件发送 HTTP 请求有什么想法吗?

xmlhttprequest outlook-addin
1个回答
0
投票

出于好奇,您使用什么操作系统?

如果是 Windows,是“经典”还是新版 Outlook?

© www.soinside.com 2019 - 2024. All rights reserved.