個人檔案懒人窝相片部落格清單 工具 說明
4月26日

一个愚蠢的错误

在练习使用AJAX和JSP的时候,编写了如下一段程序:
使用了AJAX技术的frontend,html:
<html>
<head><title>
test</title></head>
<body>
<script language= "javascript" >
var http_request = false;
function send_request( url )
{
 if( window.XMLHttpRequest )
 {
  http_request = new XMLHttpRequest();
  if( http_request.overrideMimeType )
  {
   http_request.overrideMimeType( "text/xml" );
  }
 }
 else if( window.ActiveXObject )
 {
  try
  {
   http_request = new ActiveXObject( "Msxml2.XMLHTTP" );
  }
  catch(e)
  {
   try
   {
    http_request = new ActiveXObject( "Microsoft.XMLHTTP" );
   }
   catch(e)
   {}
  }
 }
 if(!http_request)
 {
  window.alert( "Error!" );
  return false;
 }
 http_request.onreadystatechange = process_request;
 http_request.open( "get" ,url,true);
 http_request.send(null);
}
function process_request()
{
 if( http_request.readyState == 4 )
 {
  if( http_request.status == 200 )
  {
   document.getElementById( "city" ).innerHTML = http_request.responseText;
//   document.write(http_request.responseXML.getElementsByTag("body")[0]);
//   alert(http_request.responseXML.getElementsByTag("body")[0]);
  }
  else
   alert( "let me see:" +http_request.status);
 }
}
function go()
{
 zipValue = document.form1.zip.value;
 send_request( "http://localhost:8080/zip.jsp?zip=" +zipValue);
}
</script>
<form name=form1>
zip: <input type= "text" name= "zip" id= "zip" />
city: <span id= "city" >Look at me !</span>
<p>
<input type= "submit" onclick= "go()" />
</form>
</body>
</html>
 
后台处理的jsp:
<%@ page contentType= "text/html;charset=gb2312" %>
<%@ page import= "java.io.*"  %>
<html>
<head>
<title>
what ever</title>
</head>
<body>
<%
String zip = request.getParameter( "zip" );
PrintWriter output = response.getWriter();
if( "021" .equals(zip) )
 output.write( "Shanghai" );
else if( "010" .equals(zip) )
 output.write( "Beijing" );
else
 output.write( "Unknown City" );
output.close();
%>
</body>
</html>
 
理论上程序没问题,运行后发现怎么也不出结果,郁闷了一天。结果发现是在frontend的问题。最后那个input不应该用submit,因为你一点击就会把数据提交给form中action指定的位置。(虽然这个程序里面我没有设置action地址)如果用submit,然后不用onclick这个函数,比如用onmousemove,则程序运行良好,后来换用别的input类型,也没有,问题。
 
恩,值得注意阿。

回應

請稍候...
很抱歉,您輸入的回應過長。請縮短您的回應。
您尚未輸入內容,請再試一次。
很抱歉,目前無法新增您的回應,請稍後再試。
若要新增回應,您的父母必須先給您權限。要求權限
您的家長已關閉回應功能。
很抱歉,目前無法刪除您的回應,請稍後再試。
您已超過每日回應上限次數,請於 24 小時後再試一次。
由於系統顯示您可能傳送垃圾郵件給其他使用者,因此您帳號中的回應功能已遭停用。 如果您認為自己帳號遭錯誤停用,請連絡 Windows Live 支援
請完成下列安全檢查,以完成回應。
您輸入的安全檢查字元必須與圖片或音訊中的字元相符。

若要新增回應,請以您的 Windows Live ID 登入 (若您使用 Hotmail、Messenger 或 Xbox LIVE,則您已擁有 Windows Live ID)。登入


沒有 Windows Live ID?註冊

引用通告

此內容的引用通告是:
http://lancerhuang.spaces.live.com/blog/cns!AD62CFA7868DC21!398.trak
引述這則內容的部落格