我的 ajax 代码有问题,我不知道我该怎么办



这就是代码。问题是,当我按下按钮时,它什么也没做,我只想检查 ajax 是否有效。我的意思是当我按下按钮用数据提醒我时。 这是索引.php

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class = "container">
<div class = "jumbotron">
<input type = "username" name = "username" id = "username" placeholder = "username">
<input type = "password" name = "password" id = "password" placeholder = "password">
<button name = "startRegister" id = "startRegister">Register</button>
</div>
</div>
</body>
<script>
$(document).ready(function(){
$('#startRegister').click(function(event){
event.preventDefault();
var user = $('#username').val();
var pass = $('#password').val();
$.ajax({
url:"register.php",
method: "post",
data: {username: user, password: pass},
succes:function(data){
alert(data);
}
});
});
});
</script>
</html>

这是寄存器.php

<?php
echo "Welcome to first php !";
?>

带有我的文件的图像: 在此处输入图像描述

如果它对你没有任何作用,那么你可能没有达到你想要的">成功"触发器。最好打开控制台(如果您使用的是谷歌浏览器,请右键单击 ->Inspect(并在点击按钮后检查那里是否有任何消息。

在你复制的代码中,你有"成功",而触发器实际上是"成功"(双 s(

此外,您还可以扩展 AJAX 调用以执行错误处理:

data: {username: user, password: pass},
success:function(data){
alert(data);
},
error: function (request, status, error) {
alert(request.responseText);
}

相关内容

最新更新