如何开展WebBrowser控件跨域请求?控件、WebBrowser

由网友(浣筏)分享简介:正如你所知道做跨域XMLHTTP请求不允许在Internet Explorer的安全考虑。As you know doing Cross Domain XMLHTTP requests is not allowed for security reasons under Internet Explorer.我有一个W...

正如你所知道做跨域XMLHTTP请求不允许在Internet Explorer的安全考虑。

As you know doing Cross Domain XMLHTTP requests is not allowed for security reasons under Internet Explorer.

我有一个WebBrowser控件,我用 DocumentText 而不是导航来一个网址。由于当前访问有关:空白时,页面会尝试做一个请求本身或其他域我收到访问被拒绝 JavaScript错误。

I have a WebBrowser Control and I'm using DocumentText instead of Navigate to a URL. Since the current domain is about:blank when the page tries to do a request to itself or other domain I'm getting Access is denied Javascript error.

即使当我使用导航如果Javascript的一个请求到另一个域这是行不通的。

Even when I use Navigate if the Javascript do a request to another domain it doesn't work.

我怎样才能解决这个?

这个HTML code应与WebBrowser控件:

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>

我没有过的JavaScript / HTML code控制,我的应用程序只承载WebBrowser控件 *我发现了一些所谓的 CROSS_DOMAIN_DATA URL操作标志< /一>,不知道这是否是正确的方向。即使这是我不知道如何实现它。* 此外,如果你能回答这个问题:How设置当前document.domain的WebBrowser控件中,以避免出现访问被拒绝?这足以让我也。

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.

推荐答案

我发现了一个肮脏的解决方法,加载本地HTML(C: TEMP temp.html),然后通过JavaScript修改了它的内容。

I found a dirty workaround, load a local HTML (c:temptemp.html) and then modify the content of it via javascript.

在此之后有但是使用的document.write引起其他讨厌的问题,如JQuery的没有更多的跨域限制。就绪功能将无法正常工作。

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.

阅读全文

相关推荐

最新文章