jquery ajax传值到action
jsp页面:<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
check = function(){
var userName = $('#userName').attr("value");
var password = $('#password').attr("value");
if(userName == ''){
alert("User name can't be null!");
return;
}
if(password == ''){
alert("Password can't be null!");
return;
}
var url = "com/controller/CheckAction.action";
$.ajax(
type:"post",
url:url,
data:{userName:userName,password:password}
success:function(){alert("ok");}
)
}
</script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="form1" action="com/controller/CheckAction.action" method="post" enctype="multipart/form-data">
User Name:<input type="text" id="userName" name="userName"></input>用户名不能为空<br/>
Password:<input type="text" id="password" name="password"></input>密码不能为空<br/>
<input type="button" value="register" onclick="check();">
</form>
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="/tmp"></constant>
<package name="first" extends="struts-default">
<!-- 实现文件的上传 -->
<action name="upload" class="com.controller.FileUploadAction">
<interceptor-ref name ="fileUploadStack" />
<interceptor-ref name="defaultStack"/>
<result name="success">/WEB-INF/pages/showUpload.jsp</result>
</action>
<action name="CheckAction" class="com.controller.CheckAction">
<result name="success">/WEB-INF/pages/trueMessage.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
------解决方案--------------------data:"userName="+userName+"&password="+password
------解决方案--------------------楼上正解 data:"userName="+userName+"&password="+password
------解决方案--------------------var url = "com/controller/CheckAction.action";
你的URL不对吧,Action断点可以访问得到吗?
一般URL为: /namespace/CheckAction.action
------解决方案--------------------你的action如果单独访问可以访问到吗?
data:"userName="+userName+"&password="+password
参数是要这样传的。
------解决方案--------------------$.ajax(
type:"post",
url:url,
data:{userName:userName,password:password}//是不是这里你少了个逗号???????
success:function(){alert("ok");}
)
}
------解决方案--------------------