drl37530 2015-12-07 01:46
浏览 94

Jquery ajax返回一个带有responsetext的json对象为null

I am trying to use ajax in bootstrap form. I am using PHP on the serve side. The ajax method successfully posts the data but fails to receive any data. The status shown is 200 and ready state is also 4 but still the error function is executed. The PHP script has the header as application/json and uses json_encode. But it seems that the ajax never gets the object that the server sent! Please help.

this is the jquery: `

    $.ajax({ 
    url:'http://localhost/Texephyr/startbootstrap-agency-1.0.5/login.php',
    method:'POST',
    data:$('#log-in').serialize(),
    dataType:'json',
    success:function(msg){
        if(msg =="true"){
            window.location.assign("admin/pages/index.php");
                console.log(msg);
        }
        else{
            if(msg == "false-p"){
                $('#password-error').append('Invalid Password');
                console.log(msg);
            }
            else{
                $('#email-error').append('Invalid Username');
                console.log(msg);   
            }
        }
    },
     error: function(msg){
         $('#email-error').append("There was an Error!");
         console.log(msg)   
     }
});`

and this is the PHP script: `

  <?php
  header("Content-type: application/json");
  session_start();
  include("db_config.php");

  $json = array("txt" = >"false");        
  if(isset($_POST['username']) && isset($_POST['password'])){


 $query = "SELECT id,f_name,l_name,password FROM users WHERE email = '$uname'";
 $login-status = $db->query($query);
 if($login-status->num_rows ===1){
           $result = $login-status->fetch_assoc();
           if(password_verify($passd,$result["password"])){
                 $_SESSION["fname"] = $result["first_name"];
                 $_SESSION["lname"] = $result["last_name"];
                 $_SESSION["id"] = $result["id"];
                 $json["txt"]="true";
                 echo json_encode($json);

           }
     else{
         $json["txt"]="false-p";
        echo json_encode($json);  
       }
    }
     else {
         $json["txt"] = "false";
         echo json_encode($json);
          }
     }else{
               $json["txt"]="false";
               echo json_encode($json);    
             }
       ?>

`

  • 写回答

1条回答 默认 最新

  • dongpobo6009 2015-12-07 04:23
    关注

    Looks like you have to change javascript file like this

    $.ajax({ 
    url:'http://localhost/Texephyr/startbootstrap-agency-1.0.5/login.php',
    method:'POST',
    data:$('#log-in').serialize(),
    success:function(msg){
        msg=$.parseJSON(msg);
        if(msg.txt =="true"){
            window.location.assign("admin/pages/index.php");
                console.log(msg);
        }
        else{
            if(msg.txt == "false-p"){
                $('#password-error').append('Invalid Password');
                console.log(msg);
            }
            else{
                $('#email-error').append('Invalid Username');
                console.log(msg);   
            }
        }
    },
     error: function(msg){
         $('#email-error').append("There was an Error!");
         console.log(msg)   
     }
     });`
    

    You were setting the datatype as json since we are sending normal text data to php it can be eliminated. And in the response data it is better to parse the data returned to an Object by $.parseJSON()

    Then you can remove header() code from php script, code i posted will work without json headers.

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题