+ -
当前位置:首页 → 问答吧 → ajaxValidator 总是提示 服务器没有返回数据

ajaxValidator 总是提示 服务器没有返回数据

时间:2009-03-22

来源:互联网

我用ajaxValidator做表单验证用户名的时候,不论正确与否 总是提示 “服务器没有返回数据,请重试”

代码一浏览器端:
.ajaxValidator({
type : "get",
url : "regedit.php?job=check",
datatype : "json",
success : function(data){
  if( data == "1" )
{
  return true;
}
  else
{
  return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,请重试");},
onerror : "该用户名不可用,请更换用户名",
onwait : "校验中,请稍候..."
});
代码二服务器端:
if($_GET['job']=='check'){
$sql = "select user_id from cms_user where user_name='$user_name'";
$num = $db->myquery_num($sql);
if($num>0){
echo("0");
exit(0);
}else{
echo("1");
exit(0);
}

以上是主要代码,各位达人帮忙看看哪里有错

作者: gavy41   发布时间: 2009-03-22

同样问题帮顶了!!!

作者: huangjin2805529786   发布时间: 2010-01-16

关键一点,既然是用了datatype : "json",
那么服务器端必定要返回json格式
JScript code

            if( data.result == "0" )
            {
                return true;
            }
            else
            {
                return false;
            }




服务端:
PHP code

if($_GET['job']=='check'){ 
  $sql = "select user_id from cms_user where user_name='$user_name'";
  $num = $db->myquery_num($sql); 
  echo json_encode(array('result' => num );
}


作者: greedwind   发布时间: 2011-06-01

请注意,PHP里的$_GET() 应该和JS的 .ajaxValidator({type : "get",
方法一致

作者: greedwind   发布时间: 2011-06-01

这是codeignitor的参考代码
PHP code

    function ajax_checkuser()
    {
        $username = $this->input->get('txt_username');
        //echo $username;
        $where = "lower(username) = '".strtolower($username)."'" ;
        $this->db->where($where);
        $this->db->from('tblclients');
        echo json_encode(array('username'=>$username, 'result' => $this->db->count_all_results()));
    }


作者: greedwind   发布时间: 2011-06-01