如何在Webbrowser控件中执行跨域请求? [英] How to carry out Cross Domain request in a Webbrowser Control?
问题描述
您知道在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>
- 我无法控制Javascript / HTML程式码,我的应用程序只托管Webbrowser控制
- 我发现了一个名为 CROSS_DOMAIN_DATA URL操作标志,不确定是否正确的方向。
- 此外,如果您可以回答此问题:如何在WebBrowser控件中设置当前document.domain以避免访问被拒绝? 我发现了一个肮脏的解决方法,加载本地HTML(c:\temp\temp.html),然后通过javascript修改其内容。
在这之后,没有更多的CrossDomain限制使用document.write导致其他讨厌的问题,如JQuery
.ready
函数将不起作用。As you know doing Cross Domain XMLHTTP requests is not allowed for security reasons under Internet Explorer.
I have a WebBrowser Control and I'm using
DocumentText
instead ofNavigate
to a URL. Since the current domain isabout:blank
when the page tries to do a request to itself or other domain I'm gettingAccess is denied
Javascript error.Even when I use
Navigate
if the Javascript do a request to another domain it doesn't work.How can I get around this?
This HTML code should work with WebBrowser Control:
<body> <a href="javascript:getit('http://www.google.com')">this should work</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>
- I don't have control over Javascript / HTML code, my application only hosts a Webbrowser Control
- I found something called CROSS_DOMAIN_DATA URL Action Flags, not sure if it's right direction. Even it's I'm not sure how to implement it.
- Also if you can answer to this question : How to set current document.domain in WebBrowser Control to avoid "Access is denied"? that's enough for me as well.
解决方案I found a dirty workaround, load a local HTML (c:\temp\temp.html) and then modify the content of it via javascript.
After this point there is no more CrossDomain restrictions however using document.write causing other nasty problems such as JQuery
.ready
functions won't work.这篇关于如何在Webbrowser控件中执行跨域请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!