我m trying to parse a HTML result of **XmlHttpRequest** in Firefox. I
期待从 XmlHttpRequest s *responseText* but when I
接收HTML结果,并调用警报(responseText)。
我ve followed the example from http://stackoverflow.com/questions/888875/how-to-parse-html-from-javascript-in-firefox but that doesn
也工作。
这是让我自己清楚的代码:
<html>
<head>
<script type="text/javascript">
var http1;
var result;
function onPageLoad()
{
http1=getXmlHttpObject();
http1.open("GET", "https://login.yahoo.com/config/login_verify2?&.src=ym", true);
http1.send(null);
http1.onReadyStateChange=stateChanged();
}
function stateChanged()
{
if(http1.readyState==4)
{
result = http1.responseText;
alert("result"+ result);
var tempDiv = document.createElement('div');
tempDiv.innerHTML = result.replace(/<script(.|\s)*?\/script>/g, '');
// tempDiv now has a DOM structure:
alert(tempDiv.getElementById('username').size);
}
else
alert("mircea geoana la zoo");
}
function getXmlHttpObject()
{
var objXMLHttp=null;
if (typeof XMLHttpRequest!= 'undefined')
{
objXMLHttp=new XMLHttpRequest();
}
else
{
objXMLHttp=new ActiveXObject(Microsoft.XmlHttp);
}
return objXMLHttp;
}
</script>
</head>
<body onload="onPageLoad()">
<p>aaa<p>
</body>
</html>
答案 0 :(得分:2)
http1.onReadyStateChange=stateChanged();
应该是
http1.onReadyStateChange=stateChanged;
答案 1 :(得分:0)
这是你应该如何定义xmlhttp对象:
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
查看此w3School tutorial,了解如何正确使用AJAX调用等。
编辑 - 我的不好,只看到了定义IE6 / 5调用的行。无论哪种方式,这种方法都要干净得多。
答案 2 :(得分:0)
我发现那里有一个很大的错误..在其他分支上的消息应该是'miRcea',而不是'micea'。告诉我,如果这解决了你的问题,克劳迪先生;))
答案 3 :(得分:0)
您只能将AJAX请求发送到JavaScript所源自的域。而且我猜你没有从“login.yahoo.com”发送你的请求......