我想提交一个表单,而不刷新它,使用jquery和ajax,我也使用laravel。jquery AJAX POST总是结束在一个空白页
url重定向到更新表单中数据的路径。
e.preventDefault()不起作用,看到很多帖子和解决方案,这对我来说没有任何作用,我希望它不是明显的,我没有看到它。
我尝试了多种事情,仍然无法弄清楚问题在哪里。
FORM
{{ Form::open(array('method'=>'post','class'=> 'profile_form ajaxpasswordreset','url'=>'/resetpassword')) }}
<label for="old_password"> Current Password </label>
<div class="form-group">
{!! Form::password('old_password', ['class'=>'form-control']) !!}
@if ($errors->has('old_password'))
<span class="help-block">
<strong>{{ $errors->first('old_password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label for="old_password"> New Password </label>
{!! Form::password('password', ['class'=>'form-control']) !!}
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<label for="old_password"> Password confirmation </label>
{!! Form::password('password_confirmation', ['class'=>'form-control']) !!}
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
<button type="submit" id=""> Alterar Password </button>
{{Form::close()}}
JS
<script>
$(document).ready(function() {
$('ajaxpasswordreset').on('submit', function(e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: 'POST',
data: $('ajaxpasswordreset').serialize(),
url: '/resetpassword', // This is the url we gave in the route
success: function(response){
var stack_bar_bottom = {"dir1": "up", "dir2": "right", "spacing1": 0, "spacing2": 0};
new PNotify ({
title: "Sucesso",
text: "Password alterada com sucesso! ",
styling: "bootstrap3",
addclass: "stack-bottomright custom",
nonblock: {
nonblock: true
},
stack: stack_bar_bottom
});
},
error: function(jqXHR, textStatus, errorThrown) {
var stack_bar_bottom = {"dir1": "up", "dir2": "right", "spacing1": 0, "spacing2": 0};
new PNotify ({
title: "Erro",
text: "Algo correu mal, tenta novamente.",
styling: "bootstrap3",
addclass: "stack-bottomright custom",
nonblock: {
nonblock: true
},
stack: stack_bar_bottom
});
}
});
});
});
</script>
你检查控制台? –
是的,没有与此相关的错误。 –
您是否在调用ajax post之前尝试使用e.preventDefault()? – panox