struts2使用prototype.js 返回值为空
jsp:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="js/prototype.js" ></script>
<script type="text/javascript" src="js/json.js"></script>
<script type="text/javascript">
function validateName()
{
var url = 'http://localhost:8081/login/a/login.action';
var params = Form.Element.serialize('username');
var myAjax = new Ajax.Request(url,
{
method:'post',
parameters:params,
onComplete: processResponse,
asynchronous:true
});
}
function processResponse(request)
{
//var action = request.responseText.parseJSON();
//var action = eval("(" + request.responseJSON + ")");
var action = request.responseJSON;
alert(action);
$("tip").innerHTML = action.tip;
}
</script>
<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">
-->
</head>
<body>
<form id="Form" name="Form" action="login.action" method="post">
<input id="username" name="username" label="用户名" onblur="validateName();"/>
</form>
<span id="tip" style="color:red;font-weight:bold"></span>
</body>
</html>
struts.xml
<package name="a" namespace="/a" extends="json-default" >
<action name="login" class="chen.Action.LoginAction" method="login" >
<result type="json">
</result>
</action>
</package>
Action:
private String tip;//省去get,set方法
public String login()
{
User u = new User();
u.setUsername(username);
u.setPassword(password);
try
{
if (dao.exists(u))
{
setTip("系统中已有" + username + "用户名,请重新选择一个!");
}
else
{
setTip( "你好!" + username + ", 这个用户名可用!" );
}
System.out.println(tip);
}
catch (Exception e)
{setTip(e.getMessage());}
System.out.println("************");
return "success";
} --------------------编程问答-------------------- 这种问题只能自己找了,就一个这样的页面,无法断定问题。
补充:Java , Web 开发