注册 登录
编程论坛 jQuery论坛

Error:没有对象,帮忙看看,谢谢

yexin218 发布于 2007-08-15 21:17, 1439 次点击
servlet CODE:

httpServletResponse.setContentType("text/xml; charset=UTF-8");
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+"<songaddr>"+this.getSongAddr()+"</songaddr>"+"<wordaddr>"+this.getWordAddr()+"</wordaddr>";
System.out.println("*******************\n");
System.out.println(content);
System.out.println("*******************\n");
httpServletResponse.getWriter( ).write(content);

从servlet返回的xml结果是:
<?xml version="1.0" encoding="UTF-8"?>
<songaddr>http://202.38.64.71:8099/英文/Al
l_Saints/Saints_And_Sinners/daSYlaKpNw$$.mp3</songaddr>
<wordaddr>40369</wordaddr>

然后到js文件中


CODE:
window.onload = function(){
document.getElementById("songsearch").onclick=function(){
var _song= document.getElementById("song");
var _singer=document.getElementById("singer");
if(_song.value){
getSong(_song.value,_singer.value);
}

};
//defualt value
//getSong("等一分钟","");
};
function getSong(song,singer){
if(song == null){
document.getElementById("song").value="";
//return ;
}else{
var url = "http://localhost:8080/baidusongs/songsearch?song="+encodeURIComponent(song)+"&singer="+encodeURIComponent(singer);
httpRequest("GET",url,true,handleResponse);
}
}

function eMsg(msg,sColor){
var div = document.getElementById("addr_song");
div.style.color=sColor;
div.style.fontSize="0.9em";
//remove old messages
if(div.hasChildNodes()){
div.removeChild(div.firstChild);
}
div.appendChild(document.createTextNode(msg));

}
function handleResponse(){
var xmlReturnVal;
try{
if(request.readyState == 4){
if(request.status == 200){
xmlReturnVal=request.responseXML;
if(xmlReturnVal != null) {
var addr_song = xmlReturnVal.getElementsByTagName("songaddr")[0];
//var addr_song_val = addr_song.childNodes[0].data;
//eMsg(addr_song_val,"red");
addr_word = xmlReturnVal.getElementsByTagName("wordaddr")[0];
if(addr_song){
eMsg(addr_song.childNodes[0].data,"red");

//document.getElementById("addr_song").value= addr_song.chileNodes[0].data;
}
if(addr_word){
eMsg(addr_word.chileNodes[0].data,"red");
//document.getElementById("addr_word").value= addr_word.chileNodes[0].data;
}
}
}else{
alert(
"A problem occurred with communicating between"+
" the XMLHttpRequest object and the server program.");
}
}
}catch (err) {
alert("It does not appear that the server "+
"is available for this application. Please"+
" try again very soon. \nError: "+err.message);

}
}

前台html

[Copy to clipboard] [ - ]
CODE:
<body>
<h3>Input the name of song and singer</h3>
<form action="javascript:void%200" method="get">
<p>
Name of Song: <input type="text" id="song" name=
"song_name" size="20" maxlength="20" />
</p>
<p>
Name of Singer: <input type="text" id="singer" name=
"singer_value" size="20" maxlength="20" />
</p>
<p>
<div id="addr_song"></div>
<!--<div id="addr_word"></div>-->

</p>
<p>
<button id="songsearch" type="button">Search</button>
</p>
</form>

可是就是没有反应。
原因可能在



if(xmlReturnVal != null) {

var addr_song = xmlReturnVal.getElementsByTagName("songaddr")[0];

//var addr_song_val = addr_song.childNodes[0].data;

//eMsg(addr_song_val,"red");

addr_word = xmlReturnVal.getElementsByTagName("wordaddr")[0];


有时候会出现没有对象的错误!
帮忙看看,错在什么地方了?
谢谢!

[此贴子已经被作者于2007-8-15 21:24:14编辑过]

2 回复
#2
ayue2222007-08-17 11:57
XP系统吧 ?
还有var url = "http://localhost:8080/baidusongs/songsearch?song="+encodeURIComponent(song)+"&singer="+encodeURIComponent(singer);
最好用IP方式来链接 ...

只是自己的一点看法.....不知道有没有帮助 ...
#3
yexin2182007-08-17 12:19
哦,ip方式指?

1