我使用jQuery将表单信息发送到我的服务器php API。现在我需要为它写一个摩卡测试,而我对摩卡和jQuery非常陌生。我需要做什么?这里是我的代码:如何使用摩卡测试jquery post方法
<form class="form" method="post" action="http://140.115.xxx.xx/FinalProject/FetchUserData.php">
<input type="text" placeholder="Username" name ="username" id="username">
<input type="password" placeholder="Password" name ="password">
<button type="submit" id="login-button">Login</button><br>
<li id="showerror"></li>
<a id="goToRegister" href="register.html" >Register</a>
</form>
$(document).ready(function(){
var $form = $('form');
$form.submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
if(response == true) {
var username = document.getElementById('username').value;
window.location.href="main.html?" + username;
} else {
document.getElementById('showerror').innerHTML = "invalid username or password";
}
}, 'json');
return false;
});
});
仅供参考 - 您的''li'元素无效,因为它不包含在'ul'或'ol'中。 –
它只会在用户登录失败时显示。 – HenryOu
好的,但它仍然意味着您的网页的HTML将无效,这可能会影响JS,CSS和SEO排名。 –