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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料