简体   繁体   English

同一域xmlhttprequest访问被拒绝,即ie8

[英]same domain xmlhttprequest access denied ie8

I'm fairly new to the world of web development and am trying to read a txt file in internet explorer 8 and compare it to source code of a website to see if they are equal. 我是Web开发领域的新手,正尝试在Internet Explorer 8中读取txt文件,并将其与网站的源代码进行比较,以查看它们是否相等。 This is so I can work out if the web page is functioning correctly. 这样可以确定网页是否正常运行。

I managed to get the source code with an xmlhttprequest and have tried the same to get the text file (which is in the same domain as my web page) and I am getting an access denied error. 我设法使用xmlhttprequest来获取源代码,并尝试了相同的操作来获取文本文件(与我的网页位于同一域中),并且出现访问被拒绝的错误。

After some research I can see that cross-domain xmlhttprequests won't work but that's not what I'm trying to do so I'm not sure how to proceed. 经过研究后,我发现跨域xmlhttprequests无法正常工作,但这不是我要尝试的工作,因此我不确定如何进行。

Having run the same code in Firefox(current version). 在Firefox(当前版本)中运行了相同的代码。 It will read the file but not the web page! 它将读取文件,但不读取网页!

I don't mind which of the two browsers I end up using but at the moment each does half of what I want it to. 我不介意最终使用两种浏览器中的哪一种,但是目前每种浏览器都只完成我想要的一半。

my code is: 我的代码是:

function source1(){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", "http://website",true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            document.getElementById('textzone').value = xmlhttp.responseText
            var inputString = xmlhttp.responseText;
            alert(inputString);
            comparison(inputString)
        }
    }
    xmlhttp.send(null)
}

function comparison(inputString){
    xmlhttp1=new XMLHttpRequest();
    xmlhttp1.open("GET", "comparisondoc.txt", false);
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) {
            var compareString = xmlhttp1.responseText;
            alert(compareString)
            if(inputString==compareString){
                alert("Strings are equal");
            }
        }
    }
    xmlhttp.send(null)
}

All I need to know is why either the file won't open in ie8, or why the website source code shows up blank (in the alert) in firefox. 我只需要知道为什么无法在ie8中打开文件,或者为什么网站源代码在firefox中显示为空白(在警报中)。 Any help would be appreciated. 任何帮助,将不胜感激。

It could be a browser support issue. 这可能是浏览器支持问题。 Try the following code to initialize your XMLHttpRequest : 尝试以下代码初始化XMLHttpRequest:

function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}

Check your comparison function. 检查您的比较功能。 You should you xmlhttp1 instead of xmlhttp at 2 places 您应该在2个地方使用xmlhttp1而不是xmlhttp

function comparison(inputString){
    xmlhttp1=new XMLHttpRequest();
    xmlhttp1.open("GET", "comparisondoc.txt", false);
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) {
            <!--alert(xmlhttp1.responseText)-->
            var compareString = xmlhttp1.responseText;
            alert(compareString)
            if(inputString==compareString){
                alert("Strings are equal");
            }
        }
    }
    xmlhttp1.send(null)
}

Try to add the if(xmlhttp.status == 200) { } stuff. 尝试添加if(xmlhttp.status == 200){}内容。 Remember both of these are looping through status' "AND" readystates. 记住,这两个都是在状态“ AND”就绪状态之间循环。

Technically you could be erroring somewhere (I'd rather not speculate on) halting progress to next request or whatever without the status check. 从技术上讲,您可能会在某个地方出错(我宁愿不要推测),从而无法继续下一个请求的进度,或者执行任何没有状态检查的操作。

Also you "should" try other request techniques. 另外,您“应该”尝试其他请求技术。 ie.. xmlhttp.onreadystatechange = function(){itsReady(inputString)}; 即.. xmlhttp.onreadystatechange = function(){itsReady(inputString)}; // we keep this line short and simple calling to another func that contains your status and readystate checks, response stuff, and more func. //我们使该行简短而简单地调用另一个函数,该函数包含您的状态和readystate检查,响应内容以及更多函数。

On a pretty normal run the Loop looks like: hi rdySte:1///status 0//////// hi rdySte:2///status 200//////// hi rdySte:3///status 200//////// hi rdySte:4///status 200//////// 正常运行时,循环看起来像:hi rdySte:1 /// status 0 //////// hi rdySte:2 /// status 200 //////// hi rdySte:3 // / status 200 //////// hi rdySte:4 /// status 200 /////////

I ran into a lot of weird issues trying the long onreadystatechange = function (){ ... All stuff..} I successfully run a crazy set of request functionalities using the short onreadystatechange technique. 尝试使用长onreadystatechange = function(){...所有东西..},我遇到了许多奇怪的问题,我使用短onreadystatechange技术成功运行了一组疯狂的请求功能。

I noticed at the last minute-> is there a reason why the async flags are different between your funcs? 我在最后一分钟注意到->您的func之间的异步标志是否有所不同是有原因的吗? I'd set them all to true unless you have a great reason. 除非您有充分的理由,否则我都会将它们设置为true。

This will work: ( to test: 2 pages t1.php contains a num or whatever and t2.txt that has a num in sam dir as the funcs are called in ) 这将起作用:( 进行测试:2页t1.php包含num或其他内容,而t2.txt在sam dir中包含num,因为调用了funcs

function source1(){
                                            var avar = 1;
                                                xmlhttp=new XMLHttpRequest();
                                                xmlhttp.open("GET", "t1.php",true); // shortened f-names for ease of test
                                                xmlhttp.onreadystatechange = function(){jsg_snd(avar)};
                                                xmlhttp.send(null)
}

function jsg_snd(avar){
    if (xmlhttp.readyState==4) {
        if (xmlhttp.status == 200) {
                                            var inputString = xmlhttp.responseText;
                                                document.getElementById('text_zone').innerHTML = inputString;
                                                document.getElementById('text_zone1').value = inputString;
                                                // alert(inputString);//
                                                comparison(inputString)
        }
    }
}

function comparison(inputString){
        xmlhttp1=new XMLHttpRequest();
                                                xmlhttp1.open("GET", "t2.txt", true);
                                                xmlhttp1.onreadystatechange= function(){jsg_snd1(inputString);};
                                                xmlhttp1.send(null)
}

function jsg_snd1(inputString){
    if (xmlhttp1.readyState==4) {
        if (xmlhttp1.status == 200) {
                                            var compareString = xmlhttp1.responseText;
                                                        //alert(compareString)
            if(inputString==compareString){
                                                        //alert("Strings are equal");
                                                document.getElementById('text_zone').innerHTML += "; Ok "+inputString+"=="+compareString+"";
            }
        }
    }
}

Now the html in your body should look like: 现在,您体内的html应该看起来像:

    <tt id = 'text_go' onMouseUp="source1();" >Go!</tt>
    <tt id = 'text_zone' onMouseUp="text_zone.innerHTML = '';" >Click to clear!</tt>
    <input type ='text' id = 'text_zone1' onMouseUp="text_zone1.value = '';" value = 'Click to clear!' >

The extra stuf is for _ __s & giggles. 多余的stuf用于_ __s和咯咯笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM