【发布时间】:2014-03-01 13:49:41
【问题描述】:
在我的表单中,我想使用带有 $.post 的 ajax 并使用它来获取和发布请求,在使用 $.post 并提交表单后,我的页面刷新并且警告控制器验证不起作用。
仅在控制器中请求 ajax 无法正常工作
我的表格:
<script type="text/javascript">
$(document).ready(function(){
$('form').submit(function(){
name = $('#name').val();
family = $('#family').val();
email = $('#email').val();
currPassword = $('#currPassword').val();
password = $('#password').val();
password_confirmation = $('#password_confirmation').val();
$.post('{{ URL::route('admin.profile.update') }}' ,
{
name : name,
family : family,
email : email,
currPassword : currPassword,
password : password,
password_confirmation : password_confirmation
},
function(data){
if(data.errors)
alert(data.errors)
}
);
//return false;
});
})
</script>
{{ Form::model($profile, array('route' => array('admin.profile.update', $profile->id), 'method' => 'PUT')) }}
<div style='padding:10px;font-weight: bold;'>ویرایش مشخصات شخصی</div>
<table style='width:95%;border:none;font-size:11px;margin: 5px;text-align: left'>
<tr>
<td width="80px">{{ Form::label('name' , 'نام: ') }}</td>
<td>{{ Form::text('name',null , array('id'=>'name', 'class' => 'form-control' )) }}</td>
</tr>
<tr>
<td>{{ Form::label('family' , 'نام خانوادگی: ') }} </td>
<td>{{ Form::text('family', null, array('id'=>'family', 'class' => 'form-control')) }}</td>
</tr>
<tr>
<td>{{ Form::label('email' , 'ایمیل: ') }}</td>
<td>{{ Form::text('email', null, array('id'=>'email', 'class' => 'form-control ltr')) }}</td>
</tr>
<tr>
<td>{{ Form::label('currPassword' , 'رمز عبور فعلی: ') }}</td>
<td>{{ Form::password('currPassword', array('id'=>'currPassword', 'class' =>'form-control ltr')) }}</td>
</tr>
<tr>
<td>{{ Form::label('password' , 'رمز عبور: ') }}</td>
<td>{{ Form::password('password', array('id'=>'password', 'class' =>'form-control ltr')) }}</td>
</tr>
<tr>
<td>{{ Form::label('password_confirmation' , 'رمز عبور مجدد: ') }}</td>
<td>{{ Form::password('password_confirmation', array('id'=>'password_confirmation', 'class' =>'form-control ltr')) }}</td>
</tr>
<tr>
<td colspan="2">{{ Form::submit('ویرایش مشخصات', array('id'=>'submit','class'=>'btn btn-default' , 'style'=>'float:left')) }}</td>
<td></td>
</tr>
</table>
{{ Form::close() }}
我的控制器:
public function update($id)
{
if ( Request::ajax() ){
$rules = array(
'name' => 'required|alpha',
'family' => 'required',
'email' => 'required|email',
'currPassword'=> 'required',
'password' => 'required|confirmed|min:4',
'password_confirmation'=>'required',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
/*
return Redirect::to('/admin/profile')
->withErrors($validator)
->withInput();
*/
return Response::json(array(
'errors'=>$validator
));
}
}
}
我的路线:
Route::group(array('before' => 'auth'), function()
{
Route::resource ('admin/profile' , 'ProfileController', array('as'=>'profile' , 'before'=>'csrf'));
});
【问题讨论】:
-
你达到行动了吗?你有一个路由过滤器..尝试禁用它
-
@VitKos 女巫过滤器???
-
不,auth 过滤器和 csrf。
-
您遇到了什么错误。此外,正如 Vit Kos 上面所问的,它是否到达您的路线?
-
@warsite 页面正在刷新,
$.post和Request::ajax不起作用。
标签: php jquery ajax laravel laravel-4