如何在Webbrowser控件中执行跨域请求? [英] How to carry out Cross Domain request in a Webbrowser Control?

查看:385
本文介绍了如何在Webbrowser控件中执行跨域请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道在Internet Explorer下,出于安全考虑,不允许跨域XMLHTTP请求。



我有一个WebBrowser控件,我使用 DocumentText 而不是导航到URL。因为当页面尝试向自身或其他域请求时,当前域是 about:blank 访问被拒绝 Javascript错误。



即使我使用 Navigate 如果Javascript向另一个域请求



此HTML代码应与WebBrowser控件配合使用:

 < body> 

< a href =javascript:getit('http://www.google.com')>这应该可以工作< / a>
< div id =x>< / div>

< / body>

< script>
function XHConn()
{
var xmlhttp,bComplete = false;
try {xmlhttp = new ActiveXObject(Msxml2.XMLHTTP); }
catch(e){try {xmlhttp = new ActiveXObject(Microsoft.XMLHTTP); }
catch(e){try {xmlhttp = new XMLHttpRequest(); }
catch(e){xmlhttp = false; }}}
if(!xmlhttp)return null;
this.connect = function(sURL,sMethod,sVars,fnDone)
{
if(!xmlhttp)return false;
bComplete = false;
sMethod = sMethod.toUpperCase();

try {
if(sMethod ==GET)
{
xmlhttp.open(sMethod,sURL +?+ sVars,true);
sVars =;
}
else
{
xmlhttp.open(sMethod,sURL,true);
xmlhttp.setRequestHeader(Method,POST+ sURL +HTTP / 1.1);
xmlhttp.setRequestHeader(Content-Type,
application / x-www-form-urlencoded);
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&&!bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z){return false; }
return true;
};
return this;
}

function getit(url){
var xmlhttp = new XHConn();
var fnWhenDone = function(oXML){document.getElementById('x')。innerHTML = oXML.responseText; alert(oXML.responseText); };
xmlhttp.connect(url,GET,,fnWhenDone);
}

< / script>



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆