gpt4 book ai didi

javascript - 跨域postMessage问题

转载 作者:行者123 更新时间:2023-11-30 10:12:08 25 4
gpt4 key购买 nike

我有一个从另一个站点调用应用程序的主网站,因此出现了跨域问题。我正在尝试使用 window.postMessage 方法,但它似乎对我不起作用。

//This is the appi that sends the message. 
$(document).ready(function() {
solution01.ini();

});
var solution01= {


ini:function(){
window.parent.postMessage('Hello World', 'http://webappi:0000');

},
}

//this is in the Main Page that have the IFrame that calls the appi above.

$(document).ready(function() {


mainSolution.ini();
});

var mainSolution = {


ini:function(){
window.addEventListener('message', mainSolution.handleResponse, false);
},

handleResponse:function(evt) {

if (evt.origin === 'http://webappi:0000')
{
alert("I'm happy to say: "+evt.data);
}else{

return;
}
},
}

问题是没有任何警报。关于这个过程的任何指导方针,我错过了什么?附言。我知道 window.addEventListener 和与 IE 和一些旧的 Opera 浏览器交叉浏览的问题,但首先我只需要使用 firefox 获得一个简单的“Hello World”,但到目前为止没有成功。问候。

最佳答案

事实:我们的网络浏览器不允许通过跨域网络 api/wcf 调用传递参数。

我在使用 web api 时遇到了这个问题。

解决方案:对我有用的解决方案是,我修改了我的 web api 以允许参数跨域传递。我使用了这篇文章中的分步说明:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

如果它不起作用,请尝试另外通过 nuget 更新您的 ASP.Net Web API 包。

关于javascript - 跨域postMessage问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com