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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题