【发布时间】:2018-10-05 14:43:23
【问题描述】:
我的 Ajax 代码
Query(document).ready(function(){
jQuery('#password_form').click(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/changepassword') }}",
method: 'post',
data: {
password: jQuery('#password').val(),
new_password: jQuery('#new_password').val(),
password_confirmation: jQuery('#password_confirmation').val()
},
success: function(result){
console.log(result);
}});
});
});
我的控制器:
public function changepassword(Request $request){
$user = Auth::guard()->user();
$request_data = $request->All();
$validator = $this->admin_credential_rules($request_data);
if($validator->fails()) {
$errors = $validator->errors();
$errors = json_decode($errors);
return response()->json([
'success' => false,
'message' => $errors
], 422); } else {
$current_password = $user->password;
if(md5($request_data['password']) == $current_password) {
$user_id = $user->id;
$obj_user = User::find($user_id);
$obj_user->password = md5($request_data['new_password']);
$obj_user->save();
return \Illuminate\Support\Facades\Redirect::to('mujucet')
->with("modal_message_success", "Password has been changed successfully");
} else {
return \Illuminate\Support\Facades\Redirect::to('mujucet')
->with("modal_message_danger", "wong old password");
}
}
}
我有一个弹出窗口,有三个字段 1-密码 2- 新密码 3- 密码确认
在 ajax 之前,我的表单正在提交,但我想用 ajax 提交表单,所以我的页面不应该重新加载,我的成功和错误消息应该显示在我的弹出表单上,但是当我点击按钮时,它的重新加载和值也不是已提交。
我不知道我的 ajax 请求有什么问题。 非常感谢您的帮助!
在此先感谢您需要您的帮助。
【问题讨论】:
-
你的错误是什么?