我已经检查过你的代码完美运行。那怎么了?
如果您直接访问
validate.php
,则不会向其发送任何数据,因此 $_POST[]
不包含任何内容。您应该访问您的表单页面,在表单中输入一些内容并单击“登录”,您应该会收到一条警报,其中包含来自 validate.php 的响应。
通过
AJAX
,您在后台发送请求,如果您向 validate.php 发送一些数据,它将起作用,否则将不起作用。我建议您进行各种错误检查/处理。
对于您的“数据”属性,我认为您应该删除引号。
为了真正调试并找出答案,你应该像这样调试。添加这些其他属性将帮助您确定遇到的问题/错误。
$.ajax({
url: 'validate.php', //i never get to this file!
type: 'POST',
dataType: 'xml', //YOu're missing this value!!!
data: { username : username , password : password, login : login},
beforeSend: function() {
//This will execute regardless what happends before the AJAX is sent.
},
success: function(xml) {
//Will execute if no errors are present while sending AJAX
},
error: function(xml) {
//If any error happens while sending AJAX, this will be called.
},
complete: function(xHR, textStatus) {
//This will execute regardless what happens.
},
});
“beforeSend”属性将在任何内容发送到服务器进行验证之前执行。
“成功”仅在返回的数据类型中没有错误或任何与此相关的错误时才会执行。
仅当返回的数据不是 dataType 期望的数据时才会执行“Error”。
“完整”无论发生什么都会执行。