日期:2014-05-16  浏览次数:20706 次

ajax验证
在失去焦点时,不知道为什么$.post()函数没有反应,谁帮忙给看看撒

$("#code").blur(function() {
$("#code_msg").html("");
  var codeTxt = $("#code").val();
    if (codeTxt == "") {
$("#code_msg").html("验证码不能为空");
    }

$.post(
            "checkcode.action", 
            {"code" : codeTxt}, 
            function(data) {
if (data) {
   $("#code_msg").html("验证码通过");
} else {
   $("#code_msg").html("验证码错误");
}
    }, "json");
});

struts.xml中的配置
<action name="checkcode" class="org.test.action.CheckCodeAction"> 
<result type="json">
<param name="root">ok</param>
</result>
</action>

------解决方案--------------------
我在 w3school 测试的,以下是链接和我测试的代码,如果你的代码位置正确,那么确保你的action url能正确访问到

http://www.w3school.com.cn/tiy/t.asp?f=jquery_ajax_post




<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){

 $("#code").blur(function() {
$("#code_msg").html("");
  var codeTxt = $("#code").val();
    if (codeTxt == "") {
$("#code_msg").html("验证码不能为空");
    }
 $.post("/example/jquery/demo_test_post.asp",
     {"code" : codeTxt}, 
    function(data,status){
      if (data) {
   $("#code_msg").html("验证码通过");
} else {
   $("#code_msg").html("验证码错误");
}
    });
    });
});
</script>
</head>
<body>

<button>向页面发送 HTTP POST 请求,并获得返回的结果</button>