RSS
热门关键字:  数据挖掘  数据仓库  商业智能  人工智能  搜索引擎

关于struts+Ajax 的问题,为什么老是返回一个undefined 呢

来源: 作者: 时间:2008-03-11 点击:

这是人家的例子,我跑着看看的!!!!

Java code
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ´example.jsp´ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script> var req; var which; function retrieveURL(url) { if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = processStateChange; try { req.open("GET", url, true); } catch (e) { alert(´创建对象失败´); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = processStateChange; req.open("GET", url, true); req.send(null); } } } function processStateChange(){ if (req.readyState == 4) { // Complete if (req.status ==200) { // OK response document.getElementById("test").innerHTML = req.responseText; } else { alert("Problem: " + req.statustext); } } } </script> </head> <body onload="retrieveURL(´/test/example.do´)"> <dir id="test"></dir> </body> </html> action部分:ExampleAction.java /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.sbt.struts.action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * MyEclipse Struts * Creation date: 01-11-2008 * * XDoclet definition: * @struts.action validate="true" */ public class ExampleAction extends Action { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward * @throws IOException */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=gbk"); PrintWriter out = response.getWriter(); out.println("hello world!"); out.flush(); return null; } } config部分:struts-config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <data-sources /> <form-beans /> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/example" type="com.sbt.struts.action.ExampleAction" /> </action-mappings> <message-resources parameter="com.sbt.struts.ApplicationResources" /> </struts-config> web部分:web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web- app_2_4.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
数据挖掘研究院


它为什么老是弹出个对话框来显示Problem:undefined啊,我让他在action里面随便传

来显示一句话,应该怎么弄啊,谢谢大家了

alert("Problem: " + req.statustext); 
改为alert("Problem: " + req.status); 
既然执行到这句了,说明响应失败了...
if (req.status ==200) { // OK response 
document.getElementById("test").innerHTML = req.responseText; 
//status等于200时正常,可以在 <dir id="test"> </dir>  
显示Action里的那句话..
} else { 
alert("Problem: " + req.statustext); 
//status响应失败了

再好好检查下程序..

/test/example.do
先直接查看这个uri所输出的内容。

2楼,能输出结果

出来了,楼上两位辛苦了,!!!! 随便问一句,传给jsp的内容是不是一定要用out.println("hello world!");  数据挖掘研究院
这个啊,有没有别的办法,
获取的时候要用 req.responseText;???

回答完了,就加你们分了,嘿嘿,帮忙咯

scriptlet一般就用隐藏对象out来输出
也可以用标签 如: <c:out> 等

Create By Any-Extract(WL-AE) 数据挖掘实验室

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?