跨域postMessage问题

时间:2014-09-20 04:06:43

标签: javascript cross-domain postmessage

我有一个主WebSite从另一个站点调用appi,因此Cross Domain成为一个问题。我正在尝试使用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;
        }
    },
}

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

1 个答案:

答案 0 :(得分:4)

事实:我们的网络浏览器不允许通过跨域网络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包。

相关问题