我正在使用JavaScript API for Excel制作 Excel 插件。addin 的入口是addin.html
,它调用了 JS、CSS 文件。所有文件都托管在www.myexample.com
.
当我们点击插件中的一个按钮时,它会弹出一个浏览器窗口window.open("https://www.myexample.com/post/", "popup")
。带有post.html
as 模板的页面由angularjs
和构建ui-router
。服务器和客户端也托管在www.myexample.com
.
的控制器post.html
需要不时地向 发送请求addin.html
。例如,post.html
发送一个地址A1
到addin.html
,并期望接收单元的电流值A1
。使用JavaScript API for Excel 的一些异步函数可以很容易addin.html
地从 Excel 获取单元格值。而我的问题是如何在.post.html
如果我们使用$http.post
,是否可以向addin.html
没有服务器的发送请求?
var getValue = function (address) {
return $http.post('...', { address: address })
.then(function (res) {
// maybe do something with res
return res
}
}
如果我们使用postMessage
,我们可以在 中实现监听器post.html
,它们是反应式的。我无法想象如何主动发送请求并等待响应。
有人可以帮忙吗?
如果我们window.open
按照 OP 中的描述打开弹出式浏览器,我们可以在弹出式窗口和加载项之间通过 发送数据postMessage
。
如果我们打开@MohamedShakeel 建议的对话框,我们可以通过messageParent
和发送数据localStorage
。
这两种方式都有效。
我无法想象如何主动发送请求并等待响应。
我们可以通过手动做出像这里这样的承诺来等待响应,例如来自加载项的消息。因此,我们将能够定义
var getValue = function (address) {
return sendRequestByPostMessage()
.then(function () {
return waitForResponse()
.then(function (res) {
// treatTheResponse
})
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句