[英]Ajax doesn't send data
I am learning to use ajax . 我正在学习使用ajax 。 By clicking a submit button in a form, does not send data. 通过单击表单中的提交按钮,不会发送数据。
Jquery code: jQuery代码:
$('input[name="login"]').click(function() {
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
type: 'POST',
url: '/?module=login&special_content=true',
data:{'username':username,'password':password, 'action':'login'},
success: function(e) {
if(e == false)
{
}
else
{
document.location.href = e;
}
}
});
});
HTML code: HTML代码:
<section style="width: 400px; margin: 10% auto; float: none;" id="box">
<section id="box-header">
<div id="box-title">%header_login%</div>
</section>
<section id="box-content" style="float: none;">
<form method="POST" action="./">
%login_username%<br>
<input type="text" style="width: 100%;" class="input" name="username" placeholder="%login_input_user%" required/><br>
%login_password%<br>
<input type="password" style="width: 100%;" class="input" name="password" placeholder="%login_input_pass%" required/><br>
<input type="button" id="login-button" style="width: 100%; box-sizing: border-box;" class="btn btn-red" name="login" value="%login_button%" />
</form>
<center><div style="margin-top: 5px;"> %login_or% <a href="?module=register">%login_signup%</a></div></center>
</section>
Where is the error? 错误在哪里? Thanks! 谢谢!
Your issue is caused by your selectors. 您的问题是由您的选择器引起的。 $("#username")
means you want an element with the id
set to "username". $("#username")
表示您想要一个id
设置为“ username”的元素。
For your two text inputs, change name="username"
and name="password"
to id="username"
and id="password"
respectively. 对于您的两个文本输入,分别将name="username"
和name="password"
更改为id="username"
和id="password"
。
In your code the #username
and #password
don't exist thus the .val()
returns empty/null. 在您的代码中#username
和#password
不存在,因此.val()
返回空/空值。
Change the selector to input[name=
will fix the problem 将选择器更改为input[name=
将解决问题
$('input[name="login"]').click(function() {
var username = $('input[name=username]').val();
var password = $('input[name=password]').val();
$.ajax({
type: 'POST',
url: '/?module=login&special_content=true',
data:{'username':username,'password':password, 'action':'login'},
success: function(e) {
if(e == false)
{
}
else
{
document.location.href = e;
}
}
});
});
http://jsfiddle.net/dgjBh/ http://jsfiddle.net/dgjBh/
check post requests with devtools ass seen in picture 用图片中的devtools屁股检查发布请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.