问题描述
页面修改的时候用到AJAX,之后提交到自身的脚本做处理<scripttype="text/javascript">functionupdateName_fathor(nid,npid){varname=document.getElementsByName("sname"+nid)[0];varname_old=document.getElementsByName("sname_old"+nid)[0];if(name.value.length>=2){if(name.value!=name_old.value){old_name=name.value;nid_1=nid;createHttpRequest();varurl="<%=path%>/DataDictionary/dataDictionaryList.jsp?dataType=updateFathorName&name="+name.value+"&npid="+npid+"&nid="+nid+"&date="+newDate();alert(url);xmlHttp.open("GET",url,true);xmlHttp.onreadystatechange=showFathorResponse;xmlHttp.send(null);}else{alert("您要修改的内容未作什么改动!");}}else{alert("请输入类别名称,长度为2-30");}}functionshowFathorResponse(){if(xmlHttp.readyState==4){varresult=xmlHttp.responseText;alert(result);if(result=="success1"){alert("修改成功");varname_old=document.getElementsByName("sname_old"+nid_1)[0];name_old.value=old_name;}else{alert("名称不能重复,请重新修改!");}}}</script><%request.setCharacterEncoding("GBK");response.setCharacterEncoding("GBK");response.setContentType("text/html");Stringtype=request.getParameter("dataType");Stringnid=request.getParameter("nid");Stringname=request.getParameter("name");Stringnpid=request.getParameter("npid");if(type!=null&&!type.equals("")){if(type.equals("updateFathorName")){Stringresult="";booleanflag=dataDictionary.updateData(nid,name,npid);if(flag==true){result="success1";out.clear();out.print(result);out.flush();}else{result="error1";out.clear();out.print(result);out.flush();}}}%>小弟我原先是想返回success或者error来做相应的判断处理,正常情况应该是上面alert(result)的结果应该是success1或者error可结果页面返回的时候却把页面的HTML元素都打印出来
解决方案
解决方案二:
补充一下,结果打印的是error!<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>MyJSP'dataDictionary.jsp'startingpage</title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!--<linkrel="stylesheet"type="text/css"href="styles.css">--><scripttype="text/javascript">functioncheckName(){varname=document.getElementById("name").value;varnameSpan=document.getElementById("nameSpan");if(name==null||name==""){nameSpan.innerHTML="<imgsrc='<%=path%>/images/cc_error.gif'/><fontstyle='color:red'>类别名称不能为空</font>";returnfalse;}else{returntrue;}}varxmlHttp;//创建xmlHttpfunctioncreateHttpRequest(){if(window.XMLHttpRequest){//火狐xmlHttp=newXMLHttpRequest();}elseif(window.ActiveXObject){//IExmlHttp=newActiveXObject("Microsoft.XMLHTTP");}}functionisIn(){createHttpRequest();varname=document.getElementById("name").value;varnpid=document.getElementById("npid").value;if(checkName()){varurl="<%=path%>/DataDictionary/addData.jsp?name="+name+"&npid="+npid+"&dataType=checkName&date="+newDate();alert(url);xmlHttp.open("GET",url,true);xmlHttp.onreadystatechange=responseResult;xmlHttp.send(null);}}functionresponseResult(){if(xmlHttp.readyState==4){varresult=xmlHttp.responseText;varnameSpan=document.getElementById("nameSpan");if(result=="exist"){nameSpan.innerHTML="<imgsrc='<%=path%>/images/cc_error.gif'/><fontstyle='color:red'>类别名称重复!</font>";}else{nameSpan.innerHTML="<imgsrc='<%=path%>/images/cc_error.gif'/><fontstyle='color:red'>恭喜您,可以使用该名称</font>";}}}</script>
解决方案三:
在线等待高手解答
解决方案四:
这个本来就是取的请求页面的元素responseText只不过以text形式表现而已
解决方案五:
你可以把if(flag==true){把上面逻辑都包含进去,然后success不设,只在else里设error然后js判断里只判断error,其他的就算成功当然你也可以往页面隐含域设一个值,然后js读取也是可以的