[英]Passing data in ajax JQUERY
I'm creating a Login page with ajax and JQuery. 我正在使用ajax和JQuery创建一个Login页面。 Here is my code for ajax:
这是我的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: 这是ajaxLogin类:
<?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. 我已经调试了代码,并且我传递给Ajax登录的URL不能正常工作。 The values for username and password are null when i load ajaxLogin.php.
当我加载ajaxLogin.php时,用户名和密码的值为null。 What is the problem with my url?
我的网址有什么问题?
var url = 'username='+username+'&password='+password;
is responceText integer ? 是responceText整数吗? 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
}
Your url
contains data for GET
method. 您的
url
包含GET
方法的数据。 For POST
you need to pass data like this: 对于
POST
您需要像这样传递数据:
data: {username: username, password: password}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.