在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个回答



是responceText整数? 你确定吗 ? 您可以尝试</ p>

  success:function(responceText){
responceText = parseInt(responceText);
document.write(responceText);
</ code> < / pre>

也...你可以通过将它构造为一个对象来更好地传递值... </ p>

  var url = {
'username':用户名,
'密码':密码
}
</ code> </ pre>
</ div>

展开原文

原文

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
}



url </ code>包含 GET </ code>方法的数据。 对于 POST </ code>,您需要传递如下数据:</ p>

  data:{username:username,password:password} 
</ code> </ pre>
</ div>

展开原文

原文

Your url contains data for GET method. For POST you need to pass data like this:

data: {username: username, password: password}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐