Javascript 从php向jquery返回json_编码错误

Javascript 从php向jquery返回json_编码错误,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,如果用户名和密码错误,我想显示错误。我从昨天开始尝试。不知道出了什么问题。每次controll出错并显示error.trusted json.但不起作用。我在jquery ajax中使用了一个新的 <script> $(document).ready(function() { $("#LoginForm").submit(function(e) { $("#simple-msg1").html("<img src='img/loading.gif'/

如果用户名和密码错误,我想显示错误。我从昨天开始尝试。不知道出了什么问题。每次controll出错并显示error.trusted json.但不起作用。我在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>

$(文档).ready(函数(){
$(“#LoginForm”).submit(函数(e){
$(“#simple-msg1”).html(“”);
var postData=“”;
postData=$('#LoginForm').serializeArray();
var formURL=$('#LoginForm').attr(“操作”);
$.ajax({
url:formURL,
类型:“POST”,
数据:postData,
数据类型:'json',
成功:函数(数据、文本状态、jqXHR){
警报(数据错误);
如果(data.error==1){
$(“#simple-msg1”).html(“”+data.message+”
”); }否则{ $(“#simple-msg1”).html(“登录成功””; window.location=“/property/Dealer/ManageProfile.php?Login=successfull”; } }, 错误:函数(数据、文本状态){ $(“#simple-msg1”).html(“错误的用户名或密码11”); } }); e、 preventDefault();//停止默认操作 }); $(“#按钮1”)。单击(函数(){ $(“#LoginForm”).submit();//提交表单 }); }); 这是php文件


我对javascript错误的猜测是,发送的响应不是纯json格式的,而是标题。您可以做的是添加
exit()在echo json_encode…之后调用
调用

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

$(文档).ready(函数(){
$(“#LoginForm”).submit(函数(e){
$(“#simple-msg1”).html(“”);
var postData=“”;
postData=$('#LoginForm').serializeArray();
var formURL=$('#LoginForm').attr(“操作”);
$.ajax({
url:formURL,
类型:“POST”,
数据:postData,
数据类型:'json',
成功:函数(数据、文本状态、jqXHR){
警报(数据错误);
如果((data.error!=“1”){
$(“#simple-msg1”).html(“”+data.message+”
”); }否则{ $(“#simple-msg1”).html(“登录成功””; window.location=“/property/Dealer/ManageProfile.php?Login=successfull”; } }, 错误:函数(xhr、状态、错误){ $(“#simple-msg1”).html(“错误的用户名或密码11”); } }); e、 preventDefault();//停止默认操作 }); $(“#按钮1”)。单击(函数(){ $(“#LoginForm”).submit();//提交表单 }); });
如果您使用
.serialize()
而不是
.serializeArray()是否有效
?我没有尝试过这个方法。这个错误发生在哪里?在javascript或php中?如果是在php中,你能提供错误消息吗?你使用了firebug吗?如果不是,你能在firefox中安装firebug插件。然后告诉我每次ajax调用的响应主体。如果php中没有问题,我认为ajax无法找到该文件。你需要下载Zend-e剪辑PDT并安装X-Debug,然后您可以找到PHP中发生错误的位置。我认为这只是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));

?>
$(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>