返回从php到jquery的json_encode错误

时间:2013-11-25 05:40:36

标签: javascript php jquery ajax

如果用户名和密码错误,我想显示错误。我正在尝试从昨天开始。不知道什么是错的。每次控制都会出错并显示错误。请记住json.but not working.i jquery ajax中的新内容,

<script>

$(document).ready(function() {
    $("#LoginForm").submit(function(e) {
        $("#simple-msg1").html("<img  src='img/loading.gif'/>");
        var postData ="";
        postData = $('#LoginForm').serializeArray();
        var formURL = $('#LoginForm').attr("action");

        $.ajax( {
            url : formURL,
            type: "POST",
            data : postData,
            dataType:'json',
            success:function(data, textStatus, jqXHR)  {
                alert(data.error);
                if(data.error == 1) {
                    $("#simple-msg1").html('<pre><code class="prettyprint">'+data.message+'</code>< /pre>');
                } else {
                    $("#simple-msg1").html('<pre><code class="prettyprint"> Login Successfull </code></pre>');
                    window.location = "/property/Dealer/ManageProfile.php?Login=successfull";
                }
            },
            error:function(data,textStatus)  {
                $("#simple-msg1").html('<pre><code class="prettyprint"> wrong username or password 11</code></pre>');
            }
        });
        e.preventDefault(); //STOP default action
    });

    $("#Button1").click(function() {
        $("#LoginForm").submit(); //SUBMIT FORM
    });

});

</script>

这是php文件

<?php
ob_start();

if(session_id() == '')
    {
        session_start();
    }

include 'config.php';

$error = '0';
$message = 'Valid';
$redirect = 'Dealer/EditLoginDetails.php';

$myusername=$_POST['txtusername']; 
$mypassword=$_POST['txtpassword']; 

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword=mysql_real_escape_string($mypassword);

$qry = "SELECT UserName,Type_user FROM login WHERE UserName = '".$myusername."' AND password = '".$mypassword."' ";

$result = mysql_query($qry) or die ("Query failed");

$UserData = mysql_fetch_array($result);

if($UserData['UserName'] != "") {
    //echo $UserData['UserName'];

    $_SESSION['UserId'] = $myusername;

    $typ = $UserData['Type_user'];

    if ( $typ == "Dealer") { 
        header('location:/Dealer/EditLoginDetails.php');  
        //echo "dealer";
        //echo json_encode(array('success'=>'true'));
        //header('location:/Dealer/EditLoginDetails.php');
    } else if ($typ == "Individual") {
        header('location:/Dealer/EditLoginDetails.php');  

    } else {
        header('location:/Builder/managep.php'); 
    }

} else { 
    $error = '1';
    $message = 'Invalid username or password';

    // echo "forbiddnt";
    //header('HTTP/1.0 403 Forbidden');
    //echo " wrong username or password";
}
echo json_encode(array('error' => $error, 'message' => $message, 'redirect' => $redirect));

?>

2 个答案:

答案 0 :(得分:0)

我对javascript错误的猜测是,标头是发送的响应不是纯粹的json。您可以执行的操作是在exit();电话

后添加echo json_encode....来电

答案 1 :(得分:0)

请使用此代码。在ajax的条件下,您似乎正在检查错误的值。

$(document).ready(function() {
$("#LoginForm").submit(function(e) {
    $("#simple-msg1").html("<img  src='img/loading.gif'/>");
    var postData ="";
    postData = $('#LoginForm').serializeArray();
    var formURL = $('#LoginForm').attr("action");

    $.ajax( {
        url : formURL,
        type: "POST",
        data : postData,
        dataType:'json',
        success:function(data, textStatus, jqXHR)  {
            alert(data.error);
            if((data.error != '1') {
                $("#simple-msg1").html('<pre><code class="prettyprint">'+data.message+'</code>< /pre>');
            } else {
                $("#simple-msg1").html('<pre><code class="prettyprint"> Login Successfull </code></pre>');
                window.location = "/property/Dealer/ManageProfile.php?Login=successfull";
            }
        },
        error:function(xhr, status, error)  {
            $("#simple-msg1").html('<pre><code class="prettyprint"> wrong username or password 11</code></pre>');
        }
    });
    e.preventDefault(); //STOP default action
});

$("#Button1").click(function() {
    $("#LoginForm").submit(); //SUBMIT FORM
});
});
</script>