【问题标题】:How do you make this ajax example work on opera and firefox?你如何让这个 ajax 示例在 opera 和 firefox 上工作?
【发布时间】:2011-01-24 01:39:00
【问题描述】:

我有一个简单的问题,以下仅适用于 IE 7 及更高版本,我怎样才能使其也适用于 firefox 和 opera?

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.tdsoft.se/index.html",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

编辑

感谢您的所有回答,现在我还有一个关于以下代码的问题

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    return xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","index.html",true);
xmlhttp.send();


}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

现在我想在 xmlhttp.responseText(换句话说,调用函数 loadXMLDoc())中搜索关键字,例如“testfile”,如果存在多个示例“testfile_1”和 "testfile_2"....."testfile_n" 然后是 "doSomething"

喜欢这个

function searchADocument(wordToSearchFor){
int numberOfTimesWordOccurs=0;
var thePageToSearchThrough [] = loadXMLDoc();
for (i=0; i<thePageToSearchThrough.length; i++){
if(thePageToSearchThrough[i]==wordToSearchFor)
 numberOfTimesWordOccurs++;
}
If  (wordToSearchFor > 1) 
document.write("<a href="http://selnc05.go.se:8080/component_test/build/testfile_1">    testfile_1</a>"<a href="http://selnc05.go.se:8080/component_test/build/testfile_2">    testfile_2</a><a href="http://selnc05.go.se:8080/component_test/build/testfile_n">    testfile_n</a>

)

Else

window.location="http://selnc05.go.se:8080/component_test/build/testfile.html";

}

我不知道从哪里开始,因为我不知道 xmlhttp.responseText 是什么类型,我可以将它存储在一个数组中并使用 for 循环等进行扫描吗? 提前致谢。 =)

【问题讨论】:

  • 控制台是否有任何错误?
  • 刚刚检查过,至少在firefox的错误控制台中没有错误,正如我所说,它在Internet Explorer中有效,而不是在其他2个浏览器中。
  • “它不起作用”对您所感知的问题的描述不是很有帮助;请尝试详细说明您的问题。
  • 我已经回答了你,但是有人删除了我的回答!
  • 我不知道您在哪里看到潜在错误(您正在谈论的控制台)。那是随机的,自从我发布问题以来就没有检查过这个网站:/无论如何,有没有一个 Firefox 插件,你可以在页面底部的资源管理器中看到这些类型的错误?

标签: javascript html xmlhttprequest


【解决方案1】:

试试这样的:

ajax("http://www.tdsoft.se/index.html", function(data) {
    document.getElementById("myDiv").innerHTML = data;
});

代码

function getXmlHttpObject() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function ajax(url, onSuccess, onError) {
    var xmlHttp = getXmlHttpObject();
    xmlHttp.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status != 200) {
                if (typeof onError == 'function') {
                    onError(this.responseText);
                }
            }
            else if (typeof onSuccess == 'function') {
                onSuccess(this.responseText);
            }
        }
    };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return xmlHttp;
}​

【讨论】:

  • 对不起,那个在 IE 中运行良好,但在 firefox 或 opera 中不行。在某处阅读 Firefox 已禁用此功能以在您自己的服务器之外进行调用,有什么办法可以解决这个问题?
  • @Anders - 不,仅当您期望 JSON 时。有window.postMessage,但没有得到广泛支持developer.mozilla.org/en/DOM/window.postMessage
猜你喜欢
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多