我有这个HTML
<section id="login">
<form>
<input type="text" name="username" size="10" placeholder="username" />
<input type="password" name="password" size="10" placeholder="password" />
<input type="submit" name="login" value="Login" />
</form>
</section>
使用调用PHP({sys_login.php)的$.ajax()
。
$(function(login){
$('#login form').submit(function(e){
var uname = $('input[name="username"]').val();
var pwd = $('input[name="password"]').val();
var dataObject = {
command : "login",
username : uname,
password : pwd
};
$.ajax({
type : "POST",
url : "application/sys/sys_login.php",
data : dataObject,
dataType : "json",
success: function(response, status){
if (response.status===true){
docCookies.setItem("sessionid", response.sessionid);
docCookies.setItem("username", response.username);
docCookies.setItem("roles", response.roles);
location.href = "application/index.php";
}else{
console.log("false")
}
},
beforeSend:function(){
console.log("sending")
}
}).error(function() {
console.log("error");
});
e.preventDefault();
});
});
但它永远不会到sys_login.php
文件。如果我使用GET
,它将到达php文件。在调试期间,甚至控制台也没有打印任何内容。我做错了什么?
*的 * UPDATE 显然后端服务器出现故障。该脚本适用于其他计算机。谢谢你的时间。
答案 0 :(得分:2)
在将其发送给PHP之前,您应该stringify(dataObject)
。
尝试
$.ajax({
type : "POST",
url : "application/sys/sys_login.php",
data : JSON.stringify(dataObject),
dataType : "json",
)};
答案 1 :(得分:0)
你能试试吗,确保你的html中有唯一的元素Id,
<section >
<form id="login">
<input type="text" name="username" size="10" placeholder="username" />
<input type="password" name="password" size="10" placeholder="password" />
<input type="submit" name="login" value="Login" />
</form>
</section>
<script type="text/javascript">
$(function(){
$('#login').submit(function(e){
var uname = $('input[name="username"]').val();
var pwd = $('input[name="password"]').val();
var dataObject = {
command : "login",
username : uname,
password : pwd
};
$.ajax({
type : "POST",
url : "application/sys/sys_login.php",
data : dataObject,
dataType : "json",
success: function(response, status){
if (response.status===true){
docCookies.setItem("sessionid", response.sessionid);
docCookies.setItem("username", response.username);
docCookies.setItem("roles", response.roles);
location.href = "application/index.php";
}else{
console.log("false");
}
},
beforeSend:function(){
console.log("sending");
}
}).error(function() {
console.log("error");
});
e.preventDefault();
});
});
答案 2 :(得分:0)
试试这段代码:
$(function(login){
$('#login form').submit(function(e){
var uname = $('input[name="username"]').val();
var pwd = $('input[name="password"]').val();
var dataObject = '{
"command" : "login",
"username" : "'+uname+'",
"password" : "'+pwd+'"
}';
$.ajax({
type : "POST",
url : "application/sys/sys_login.php",
data : dataObject,
dataType : "json",
success: function(response){
if (response.status===true){
docCookies.setItem("sessionid", response.sessionid);
docCookies.setItem("username", response.username);
docCookies.setItem("roles", response.roles);
location.href = "application/index.php";
}else{
console.log("false")
}
},
beforeSend:function(){
console.log("sending")
}
}).error(function() {
console.log("error");
});
e.preventDefault();
});
});