[英]$.ajax Post Request not sending POST data
I'm trying to streamline my login system by using ajax to handle to back and form requests to the server. 我试图通过使用ajax来处理向服务器返回请求和形成请求的方式来简化我的登录系统。 I want to POST my form data to a PHP page, then return true/false and 'Not Enough Data' if the correct parameters didn't get passed.
我想将表单数据发布到PHP页面,然后如果未传递正确的参数,则返回true / false和'Not Enough Data'。
My problem is that my AJAX request isn't sending the data that I'm giving it to send. 我的问题是我的AJAX请求没有发送我要发送的数据。
Here is my JavaScript function: 这是我的JavaScript函数:
function confirm_login()
{
val_UName = document.login_form.username.value;
val_Pwd = document.login_form.password.value;
var result = null;
var scriptUrl = "login_confirm.php";
$.ajax({
url: scriptUrl,
type: 'post',
data: ({username : val_UName, password : val_Pwd}),
dataType: 'html',
async: false,
success: function(data) {
result = data;
}
});
result = result.split('|');
if(result[1] == 'true')
window.location = "index.php";
else
alert('Could not log you in!\n\n'+result[0]);
}
This function is called in the onclick event of the submit button on the form. 在表单上的“提交”按钮的onclick事件中调用此函数。 Now, I've used the debug tools in Chrome and I KNOW that val_UName and val_Pwd are getting the form values.
现在,我使用了Chrome中的调试工具,并且知道val_UName和val_Pwd正在获取表单值。 So it's definitely a breakdown between here and the login_confirm.php
因此,这绝对是此处和login_confirm.php之间的故障。
Just for completeness' sake, here's what's going on in login_confirm.php (I left out the lines that are connecting to the DB and closing the connection): 为了完整起见,这是login_confirm.php中发生的事情(我省略了连接到数据库并关闭连接的行):
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
if($username == '' || $password == '')
echo 'Not Enough Data|';
$login_query = "SELECT * FROM users WHERE u_name = '$username' AND pwd = '$password'";
$result = mysql_query($login_query);
$result_rows = mysql_num_rows($result);
if($result_rows > 0)
{
session_start();
$row = mysql_fetch_assoc($result);
$_SESSION['loggedin'] = true;
$_SESSION['uname'] = $row['u_name'];
$_SESSION['uID'] = $row['pkid'];
$_SESSION['email'] = $row['email'];
$_SESSION['roleID'] = $row['fk_roleid'];
echo 'true';
}
else
{
echo 'false';
}
Anyone know what's going on here or seen anything like this before? 有人知道这里发生了什么吗,或者以前看过类似的东西?
PS. PS。 I've even tried changing to using GET and that still doesn't work...
我什至尝试更改为使用GET,但这仍然行不通...
result = result.split('|');
if(result[1] == 'true')
window.location = "index.php";
else
alert('Could not log you in!\n\n'+result[0]);
is run before the callback returns. 在回调返回之前运行。
Try putting that logic inside the success callback or define a success function with that logic inside. 尝试将该逻辑放入成功回调中,或使用该逻辑定义成功函数。
Code under the ajax request won't wait for the ajax request to be completed before it is being executed. Ajax请求下的代码在执行之前不会等待ajax请求完成。 So I think your ajax request is working, but nothing happens with the data because that piece of code was executed already.
因此,我认为您的ajax请求正在运行,但是数据没有任何反应,因为该代码段已被执行。
My suggestion is: 我的建议是:
function confirm_login()
{
val_UName = document.login_form.username.value;
val_Pwd = document.login_form.password.value;
var scriptUrl = "login_confirm.php";
$.ajax({
url: scriptUrl,
type: 'post',
data: ({username : val_UName, password : val_Pwd}),
dataType: 'html',
async: false,
success: function(data) {
runCode(data);
}
});
function runCode(var result){
result = result.split('|');
if(result[1] == 'true')
window.location = "index.php";
else
alert('Could not log you in!\n\n'+result[0]);
}
}
}
Edit: Function shouldn't be whithin the other function, but I think you can fix that yourself. 编辑:函数不应该在其他函数中,但是我认为您可以自己修复。
Don't wrap your data in parentheses. 不要将数据包装在括号中。 Just the {} will suffice.
仅{}就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.