dongyi2993 2015-01-01 20:18
浏览 54

在ajax JQUERY中传递数据

I'm creating a Login page with ajax and JQuery. Here is my code for ajax:

<script>
    $(document).ready(function() {
        $('#login').click(function(){
            var username=$("#username").val();
            var password=$("#password").val();
            var url = 'username='+username+'&password='+password;
            if($.trim(username).length>0 && $.trim(password).length>0){
                $.ajax({
                    type: "POST",
                    url: "ajaxLogin.php",
                    data: url,
                    cache: false,
                    success: function(responceText){
                        document.write(responceText);
                        if(responceText==1){
                            document.write('____Welcome____');
                        }
                        else if(responceText==0){
                            document.write('____Login Failed____');
                        }
                        else if(responceText == -1){
                            document.write('____Some Thing went wrong____');
                        }
                    }
                });
            }
            return false;
        }); 
    });
</script>

And here is the ajaxLogin class:

<?php
  include("db.php");
  session_start();
  if(isSet($_POST['username']) && isSet($_POST['password'])){
     $username=mysqli_real_escape_string($db,$_POST['username']); 
     $password=md5(mysqli_real_escape_string($db,$_POST['password'])); 
     $result=mysqli_query($db,"SELECT user_id FROM users WHERE user_name='$username' and user_pass='$password'");
     $count=mysqli_num_rows($result);

     $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
        if($count==1) echo 1;
        else echo 0;
     }
     else{
        echo -1;
     }
     ?>

I've debugged the code and i thing the url which i'm passing to the ajax login is not working fine. The values for username and password are null when i load ajaxLogin.php. What is the problem with my url?

var url = 'username='+username+'&password='+password;
  • 写回答

2条回答

  • douhanzhen8927 2015-01-01 20:20
    关注

    is responceText integer ? are you sure about it ? you can try like

                    success: function(responceText){
                        responceText = parseInt(responceText);
                        document.write(responceText);
    

    also... you can pass values in better way by construct it as an object...

    var url = {
        'username': username,
        'password': password
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办