【在线求助】JQuery中利用ajax验证邮箱是否存在,无法访问action,求助急急急!!! - Web 开发 / Ajax
页面的代码如下:
<%@page contentType="text/html;charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用户注册 - 当当网</title>
<link href="${pageContext.request.contextPath }/css/login.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath }/css/page_bottom.css" rel="stylesheet" type="text/css" />
</head>
<script src="../js/jquery-1.4.3.js" type="text/javascript"></script>
<script>
$(function(){
$("form :input.c").each(function(){
var $required = $("<strong style='color:red'> *</strong>");
$(this).after($required);
});
$('form :input').blur(function(){
var $parent = $(this).parent();
$parent.find(".formtips").remove();
//验证用户名
if( $(this).is('#email') ){
if( this.value == ""
|| (this.value != "" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value)) ){
var errorMsg = '请检查邮箱格式';
$parent.append('<span style="color:red" class="formtips onError">'+errorMsg+'</span>');
}else{
$.post("check.action",{data : $('#email').val()},
function(msg){
var okMsg=msg;
$parent.append("");
$parent.append('<span style="color:red"
class="formtipsonSuccess">'+okMsg+'</span>');
})
}
}
//提交,最终验证。
$('#btnClientRegister').click(function(){
$("form :input.c").trigger('blur');
var numError = $('form .onError').length;
if(numError){
return false;
}
});
});
</script>
<body>
<%@include file="../common/head1.jsp"%>
<div class="login_step">
注册步骤:
<span class="red_bold">1.填写信息</span> > 2.验证邮箱 > 3.注册成功
</div>
<div class="fill_message">
<s:form name="ctl00" method="post" namespace="/user" action="regist.action" id="f" theme="simple">
<s:token></s:token>
<h2>
以下均为必填项
</h2>
<table class="tab_login">
<tr>
<td valign="top" class="w1">
请填写您的Email地址:
</td>
<td class="a">
<input type="text" class="c" name="user.email" id="email" cssClass="text_input" />
<label>
请填写有效的Email地址<br />
</label>
</td>
</tr>
</table>
<div class="login_in">
<s:submit id="btnClientRegister" cssClass="button_1" value="注册"></s:submit>
</div>
</s:form>
</div>
<%@include file="../common/foot1.jsp"%>
</body>
</html>
struts2的配置文件:
<action name="check" class="org.tarena.action.user.CheckAction" method={isReg()}></action>
处理的action:
package org.tarena.action.user;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.tarena.dao.UserDAO;
import org.tarena.dao.impl.UserDAOImpl;
import org.tarena.pojo.User;
import com.opensymphony.xwork2.ActionSupport;
public class CheckAction extends ActionSupport implements ServletResponseAware,ServletRequestAware{
private User user;
UserDAO u=new UserDAOImpl();
public void isReg() throws SQLException
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
try {
PrintWriter out = response.getWriter();
String email=user.getEmail();
boolean b=u.hasUser(email);
if(b){
out.write("邮箱可以使用");
}
out.write("邮箱被占用");
} catch (IOException e) {
e.printStackTrace();
}
}
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
}
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
}
}
求高玩解答
[解决办法]
$.post 方法需要指定类型吧
$.post(url,"data":data,function(result){
},"json")
标红得地方。有不同的类型可以选择,这里我只是写了个json。