weixin_33730836 2015-03-22 18:41 采纳率: 0%
浏览 39

Ajax PhoneGap总是错误

I must make an application, and I've decided to use PhoneGap, but my Ajax always say "ERROR", and I don't know why, because the insert works very well...

$("#test").click(function() {
    var name = $("#name").val();
    var password = $("#password").val();
    alert(name+' '+password);
    $.ajax({
        type: "POST",
        url: "http://191.165.1.16/PULZ/ajax_action.php",
        // contentType: "application/json; charset=utf-8",
        dataType: "json",
        data : {
            actionname : 'insert',
            name:name,
            password:password
        },
        success: function(data) {
            alert("work");
        },
        error: function(data) {
            alert("There was an error loading the feed");
        }
    });
});

And my PHP code

if (isset($_POST["actionname"]) && !empty($_POST['actionname'])){
    $actionname = $_POST['actionname'];

    if($actionname == 'insert'){
        $connect = new PDOsql();
        $name = $_POST['name'];
        $password = md5($_POST['password']);
        $sql="INSERT INTO user(name,password) VALUES(?,?)";
        $opt = array($name, $password);
        $connect->query($sql,$opt);

        $connect = null;

        die(
            json_encode(
                array(
                    'state'=>'success'
                )
            )
        );
    }
}
  • 写回答

2条回答 默认 最新

  • 斗士狗 2015-03-22 23:34
    关注

    You can't use "empty()" with a non variable element, you will receive a internal server error "500", always.

    Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

    PHP Manual - empty

    评论

报告相同问题?