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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题