ajax jquery总是运行Error;

时间:2016-03-14 07:02:17

标签: javascript jquery ajax

Ajax jquery总是运行错误函数,虽然运行成功函数,但我可以得到会话值,我无法运行window.location="profile.php";

$(document).ready(function(){
    $("#login").click(function(){
    var username=$("#usern").val();
    var password=$("#user").val();
    $.ajax({
        type: "POST",
        url: "model/user.php",
        data: {
            user_log : username,
            password : password
        },
        dataType: 'json',
        error: function (xhr,textStatus,errorThrown) {

              $("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
        },
        success: function(json){    

                window.location="profile.php";

        },
        beforeSend:function()
        {
            $("#error").html("<img src='http://www.chinesecio.com/templates/base/images/loading.gif' /> Loading...")
        }
      });
      return false;
    });
});

user.php的

<?php 
ob_start();
session_start(); 
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
require_once(dirname(__FILE__).'/../model/connect.php');
?>
<?php
global $pdo;

    if(isset($_POST['user_log'])) {
        // username and password sent from Form
        $username=$_POST['user_log']; 
        $password=$_POST['password']; 
        $qr= "SELECT * FROM user where username='$username' AND password='$password'" ;
        $stmt= $pdo->query($qr);
        $row= $stmt->fetch(PDO::FETCH_ASSOC);
        if($stmt->rowCount() > 0)
        {
            $_SESSION['id']=$row['id'];
            $_SESSION['name_mem']=$row['username'];
            $_SESSION['level_mem']=$row['level'];
        }
        header("location:../../../../index.php");
    }
?>

2 个答案:

答案 0 :(得分:0)

你可以像这样使用ajax,

   <script>
        $("#login").click(function(){

         var username=$("#usern").val();
         var password=$("#user").val();
        $.ajax({
                    xhr: function() {
                        var xhr = new window.XMLHttpRequest();
                        //progress        
                        xhr.upload.addEventListener("progress", function(e) {
                            //progress value : you can load progress bar in here                            
                        }, false);
                        return xhr;
                    },
                    type: "POST",
                    url: "model/user.php",
                    data: {'username' : username, 'password' : password},
                    dataType:json,
                    success: function(msg) {
                       //when success //200 ok
                     if(msg.status=="done"){
                    window.location="profile.php";                     
                      }else{    
                       $("#error").html("<span style='color:#cc0000'>Error:</span> "+msg.massage); 
               }

                    },
                error: function(jqXHR, textStatus, errorThrown) {
                   //when error: this statement will execute when fail ajax 
                }
            });
    });
    </script>
像这样的

服务器端代码(在user.php内),

         $username=$_POST['username']; 
         $password=$_POST['password']; 

         ...........


        //$status="fail" or "done"
        //success must be always success
        //$massage= "password or username not match"
$respond=array("success"=>"success","status"=>$status,"massage"=>$massage);
       echo json_encode($respond);
       exit;

我希望你有用。

答案 1 :(得分:0)

删除此行:

header("location:../../../../index.php");

如果以上不起作用,请从ajax属性中省略:

dataType: 'json',