douzi2785 2016-11-22 18:16
浏览 33
已采纳

将数据从AJAX发送到PHP

My problem is that I always get the "failed" status, even though I put in the right values while logging on. However, when I modify my $sql in the login.php code into right values from database, I get "success" status. I thought that maybe there is something wrong with sending data from AJAX to PHP. Could you please help me how to figure it out?

This is login.php code:

header('Access-Control-Allow-Origin: *'); 
include "database.php"; 
header("Content-Type: application/json");

$numer=$_POST['number'];
$pass=$_POST['pass'];
$sql="SELECT * FROM `Users` WHERE Phone = '$numer' AND Password = '$pass'";

if ($result=mysqli_query($db,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);

 if($rowcount==1)
 {
  while($row = $result->fetch_assoc()) {
       $dane = array("status" => "success",
    "ID" => $row["ID"],
    "Name"=> $row["Name"],
    "Surname"=> $row["Surname"],
    "Phone"=> $row["Phone"],
    "Password"=> $row["Password"],
    "Admin"=> $row["Admin"],
    "Class"=> $row["Class"],
    "Banned"=> $row["Banned"]
    );

 }
  echo json_encode($dane);
 }
 else
 {
  $dane = array("status"=>"failed");
  echo json_encode($dane);
 }

  mysqli_free_result($result);
  }

mysqli_close($db);

?>

This is the AJAX code:

$("#login").click(function(){
      var number=$("#number").val();
      var pass=$("#password").val();
       if($.trim(number).length>0 & $.trim(pass).length>0)
       {
            $.ajax({
            type: "POST",
            url: "http://smietana.mechanikrac.website.pl/cgi-bin/janeta/login.php",
            data: { number:number, pass:pass}, 
            contentType:"application/json; charset=utf-8", 
            dataType:'json', 
            crossDomain: true,
            cache: false,
            beforeSend: function(){ $("#login").html('Connecting...');
            alert(number+pass);},

            success: function(data){
                alert('test'+data["status"]);

            if(data["status"]=="success")
            {
                localStorage.login="true";
                localStorage.number=number;

                alert(data["status"]);

                window.location.href = "order.html";

                $('#witaj').html(number);

            }

            else if(data["status"]=="failed")
       {
                alert("Błędne Dane");

                $("#login").html('Login again');

                }
                }
      });
    }return false;
   });
  • 写回答

1条回答 默认 最新

  • doucheng1063 2016-11-22 18:33
    关注

    Change the data to

       data: { 'number':number, 'pass':pass}
    

    And remove contentType and dataType

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码