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

Is it possible to use the postMessage() method in Javascript to do cross-domain POST, GET, PUT, etc. ca

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;
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744680037a4587563.html

相关推荐

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

    Is it possible to use the postMessage() method in Javascript to do cross-domain POST, GET, PUT, etc. ca

    3天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信