最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest - How do I use postMessage() in Javascript? - Stack Overflow

programmeradmin1浏览0评论

Is it possible to use the postMessage() method in Javascript to do cross-domain POST, GET, PUT, etc. calls? If so, how? And how do I pass headers and data?

Is it possible to use the postMessage() method in Javascript to do cross-domain POST, GET, PUT, etc. calls? If so, how? And how do I pass headers and data?

Share Improve this question edited Nov 17, 2011 at 18:53 Anne 27.1k9 gold badges67 silver badges71 bronze badges asked Nov 17, 2011 at 18:51 GroppeGroppe 3,87913 gold badges46 silver badges68 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This is a two way implementation, meaning that the page you want to call needs to have a callback that listens to such a message and give an appropriate response. You can't simply use it as a swap replacement for AJAX. The best method for that is to use a server-side proxy.

See this page for an explanation of how postMessage works.

Yes, it is possible.

There is a nice demo of what exactly you want, here

document.getElementById("iframe").contentWindow.postMessage(
        document.getElementById("message").value,
        "http://anotherdomain."
);

handled on the second side with

window.onmessage = function(e){
  if ( e.origin !== "http://html5demos." ) {
    return;
  }

  document.getElementById("test").innerHTML = e.origin + " said: " + e.data;
};
发布评论

评论列表(0)

  1. 暂无评论