在客户端使用简单的监听器/客户端模式来实现跨域后端消息传递。
let user = await getUser(data.id);
return { id: data.id, name: user.name, }; });
```javascript
try {
let { source, origin, data } = await postRobot.send(someWindow, `getUser`, {
id: 1337,
});
console.log(source, origin, "Got user:", data.name);
} catch (err) {
console.error(err);
}
For security reasons, it is recommended that you always explicitly specify the window and domain you want to listen to and send messages to. This creates a secure message channel that only works between two windows on the specified domain:
postRobot.on(
"getUser",
{ window: childWindow, domain: "http://zombo.com" },
function (event) {
return {
id: event.data.id,
name: "Frodo",
};
}
);
postRobot
.send(someWindow, "getUser", { id: 1337 }, { domain: "http://zombo.com" })
.then(function (event) {
console.log(event.source, event.origin, "Got user:", event.data.name);
})
.catch(function (err) {
console.error(err);
});
Post robot lets you send across functions in your data payload, fairly seamlessly.
For example:
postRobot.on("getUser", function (event) {
return {
id: event.data.id,
name: "Nogbad the Bad",
logout: function () {
currentUser.logout();
},
};
});
postRobot.send(myWindow, "getUser", { id: 1337 }).then(function (event) {
var user = event.data;
user.logout().then(function () {
console.log("User was logged out");
});
});
…
[ Parent page ]
+---------------------+ [ Popup ]
| xx.com |
| | +--------------+
| +---------------+ | | yy.com |
| | [iframe] | | | |
| | | | | |
| | yy.com/bridge | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | +--------------+
| +---------------+ |
| |
+---------------------+
a. Use the special ie build of post-robot: dist/post-robot.ie.js.
b. Create a bridge path on the domain of your popup, for example http://yy.com/bridge.html, and include post-robot:
c. In the parent page on xx.com which opens the popup, include the following javascript:
Now xx.com and yy.com can communicate freely using post-robot, in IE.
暂无开放 Issues,或尚未同步最近议题。