我无法使用 livevalidation.js 捕捉 ajax 请求是真还是假。无论php函数执行为false还是true,它总是返回用户名;
PHP 代码
public function valid_username()
{
$is_username_exist = 'some query here';
if($is_username_exist == true){
$output['success'] = true;
}else{
$output['success'] = false;
}
echo json_encode($output);
}
JS代码
$(document).ready( function() {
var username = new LiveValidation('username',{validMessage: 'Available', wait:500});
username.add( Validate.Presence );
username.add( Validate.Length, { minimum: 4, maximum: 10 } );
username.add( Validate.Custom, { against: function() { return check_username_availability( 'username', '#username', base_url+'cms-profile/profile_ajax/valid_username');}, failureMessage: 'Username already taken' } );
});
function check_username_availability(name, id, postUrl)
{
var dataVal = name+'='+$(id).val();
$.ajax({
url: postUrl,
cache: false,
type: 'post',
dataType: 'json',
data: dataVal,
async: false,
success: function(data) {
return $.parseJSON(data);
}
});
}