开发者

make a response on server side with php to a jquery ajax call

im trying to implement the classic username availability checker with jquery and ajax. here's my approach:

jquery, on client side:

$("#usrnameTBox").keyup(function(){
    if ($(this).val().length > 2) {
    $.ajax({
        type: 'POST',
        url: 'ajax/ajaxPerfil.php',
        data: { function : "askForUsr", usrname: $(this).val() },
        dataType: 'json',
        success: function(data) {
            alert(data);
            if(data.exists) {
                $(".usrSt").attr('style', '')
                    .attr('style', "color:red;")
                    .html('YA ESTÁ EN USO');
            } else {
                $(".usrSt").attr('style', '')
                    .attr('style', "color:green;")
                    .html('Disponible!!!');
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert("Error!!! =/");
        }
    });
    } else {
        $(".usrSt").attr('style', '')
            .attr('style', "display:none;")
            .html('');
    }
});

php, on server side:

<?php
    require_once("../src/ajax/ajaxPerfilController.php");
?>

ajaxPerfilController.php:

<?php
$srcFolder = "../src/";
$classes = array("mappers/Perfil.php",
                 "fachadas/PerfilFachada.php"
                 );
foreach ($classes as $class)
  require_once($srcFolder.$class);

function getByAttr($attr, $value) {
  if (strcmp($attr,"usrname") == 0) {
    $fachada = PerfilFachada::singleton();
    return $fachada->exists($value);
  }
  return NULL;
}

if (isset($_POST["function"])) {
  if (strcmp($_POST["function"],"askForUsr") == 0) {
    if (isset($_POST["usrname"])) {
      if (getByAttr("usrname",$_POST["usrn开发者_JS百科ame"])) {
        $return["exists"] = True;
        echo json_encode($return);
      } else {
        $return["exists"] = False;
        echo json_encode($return);
      }
    }
  }
}
?>

The thing is, it keeps telling me server error 500!!! the require routes are ok since i've harcoded the request and tried it on the console... So, i dont know what else to do, i've tried with dataType 'html', 'text', 'json' in the ajax call, but got the same result. I gess its just a simple thing, but i just dont know what to do...

just in case, im using google-chorme

thanks for your help!!! =)


I've tested your code and it should work mostly. Only thing I couldn't test is this:

$srcFolder = "../src/";
$classes = array("mappers/Perfil.php",
                 "fachadas/PerfilFachada.php"
                 );
foreach ($classes as $class)
  require_once($srcFolder.$class);

since I didn't have those files. I'm assuming this is the root of your problem.

Try this code and see if it works.

<?php
/*
$srcFolder = "../src/";
$classes = array("mappers/Perfil.php",
                 "fachadas/PerfilFachada.php"
                 );
foreach ($classes as $class)
  require_once($srcFolder.$class);
*/
function getByAttr($attr, $value) {
  if (strcmp($attr,"usrname") == 0) {
//    $fachada = PerfilFachada::singleton();
//    return $fachada->exists($value);
      return true;
  }
  return NULL;
}

if (isset($_POST["function"])) {
  if (strcmp($_POST["function"],"askForUsr") == 0) {
    if (isset($_POST["usrname"])) {
      if (getByAttr("usrname",$_POST["usrname"])) {
        $return["exists"] = True;
        echo json_encode($return);
      } else {
        $return["exists"] = False;
        echo json_encode($return);
      }
    }
  }
}
?>

if it does then the error is definitely in one of those two files


There are some reasons because you can get a 500 Error, including execution/compiling time errors.

The only error I see is that you're not declaring your $return var (well, php doesn't need declaration, but needs an array when you access with brackets), i'd add $return = array(); before using it.

BUT, You should debug your code to find the error. Depending on your tools, the best would be to set a breakpoint and start debugging. If you can't do this for any way, my advise is to use Firebug for Firefox and see what error are you receiving (in the Net panel, Response section).

Hope this helps. Cheers

PS: If you have logs in your server, you could find the error there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜