Javascript 从表单中找出url的post请求而无需操作
我试图找出一个登录表单的post请求,基本上如下所示:Javascript 从表单中找出url的post请求而无需操作,javascript,html,Javascript,Html,我试图找出一个登录表单的post请求,基本上如下所示: <form method="post"> <table class="table"> <tbody> <tr> <td>User</td>
<form method="post">
<table class="table">
<tbody>
<tr>
<td>User</td>
<td><input type="text" class="form-control form-control-sm" name="user" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" class="form-control form-control-sm" name="pass" /></td>
</tr>
<tr>
<td colspan="2" class="text-right"><button type="submit" class="btn btn-primary">Login</button></td>
</tr>
</tbody>
</table>
</form>
所以我试着:
但是,这不起作用,它会将我重定向回登录页面
我无法找出我的方法有什么问题1)不要在URL中使用任何敏感数据(凭据、密码)
2) 如果缺少操作,则表单会提交到自身,这就是表单提交到同一页面的原因
您必须指定操作
使用JavaScript或jquery函数,以下是jquery的示例:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'file',
data: $('form').serialize(),
success: function () {
alert('form submitted');
}
});
});
});
</script>
$(函数(){
$('form')。在('submit',函数(e){
e、 预防默认值();
$.ajax({
键入:“post”,
url:'文件',
数据:$('form')。序列化(),
成功:函数(){
警报(“提交的表格”);
}
});
});
});
您使用写入querystring的请求参数在GET请求中发送了登录凭据。要使其正常工作,服务器需要一个端点,在请求URL处侦听带有user和pass参数的GET请求。显然,它没有。我在postman中用一个post请求创建了相同的url,但它不起作用。for有动作吗?没有。我不知道一个表单不能有一个动作,直到今天还可以发布
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'file',
data: $('form').serialize(),
success: function () {
alert('form submitted');
}
});
});
});
</script>