Laravel 5.2 ajax 更新问题
Posted
技术标签:
【中文标题】Laravel 5.2 ajax 更新问题【英文标题】:Laravel 5.2 ajax update issue 【发布时间】:2017-01-17 10:43:33 【问题描述】:我根本无法让它工作。
我在这里做错了什么?谢谢! :-)
验证器返回字段是必需的,我似乎无法访问任何数据。
控制器:
public function updateGlobalData(Request $request)
$validator = Validator::make($request->all(), [
'company_name' => 'required',
'adress' => 'required',
'city' => 'required',
'zip' => 'required',
'country' => 'required',
'cvr' => 'required',
'email' => 'required|email',
'phone' => 'required'
]);
$errors = $validator->errors();
if ($validator->fails())
return response()->json([
'success' => false,
'message' => $errors
], 422);
$globalData = GlobalData::find($request->id)->update($request->all());
$response = [
'status' => 'success',
'msg' => 'Hotel created successfully'
];
// Session::flash('success', trans('Global data was updated'));
return \Response::json( $response );
路线:
Route::post('admin/site-settings/global-data/update', ['as' => 'admin/global-data-update', 'uses' => 'admin\GlobalDatasController@updateGlobalData']);
型号:
class GlobalData extends Model
protected $fillable = ['company_name', 'address', 'city', 'zip', 'country', 'cvr', 'email', 'phone', 'logo' ];
阿贾克斯:
function update ()
var url = 'route('admin/global-data-update')';
var part = ' $globalData->part ';
var data = $("#update_form").serialize();
var jqxhr = $.post(url,
data: data,
part: part
,
function()
)
.done(function()
var json_response = jqxhr.responseText;
if(json_response)
load_data('route('admin/global-data-edit')', ' $globalData->part ');
else
console.log("Error");
)
还有我的表格:
!! Form::model($globalData, ['id' => 'update_form']) !!
<div class="form-group">
!! Form::text('company_name', null, array('class' => 'form-control', 'placeholder' => trans('Company name'))) !!
</div>
<div class="form-group">
!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => trans('Address'))) !!
</div>
<div class="form-group">
!! Form::text('city', null, array('class' => 'form-control', 'placeholder' => trans('city'))) !!
</div>
<div class="form-group">
!! Form::text('zip', null, array('class' => 'form-control', 'placeholder' => trans('zip'))) !!
</div>
<div class="form-group">
!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => trans('country'))) !!
</div>
<div class="form-group">
!! Form::text('cvr', null, array('class' => 'form-control', 'placeholder' => trans('cvr'))) !!
</div>
<div class="form-group">
!! Form::text('email', null, array('class' => 'form-control', 'placeholder' => trans('email'))) !!
</div>
<div class="form-group">
!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => trans('phone'))) !!
</div>
<div class="form-group">
Form::hidden('id')
</div>
!! Form::button(trans('Save changes'), ['class' => 'pull-right btn btn-success save', 'id' => 'save' ]) !!
!! Form::close() !!
我真的很想得到一些帮助,感谢您的宝贵时间! :-)
【问题讨论】:
从浏览器控制台看您的网络日志如何? 你的 AJAX 语法在我看来是错误的 @MartinHenriksen,它只是遍历所有输入并说它是必需的。如果我注释掉 valdiator,此错误会出现在网络选项卡的预览中。 gyazo.com/c19d31b37d28600b0a6e8df3b07800aa 您可以在浏览器开发工具的网络选项卡中看到您发送到服务器的内容 【参考方案1】:地址字错误
$validator = Validator::make($request->all(), [
'company_name' => 'required',
'adress' => 'required',
'city' => 'required',
'zip' => 'required',
'country' => 'required',
'cvr' => 'required',
'email' => 'required|email',
'phone' => 'required'
]);
【讨论】:
以上是关于Laravel 5.2 ajax 更新问题的主要内容,如果未能解决你的问题,请参考以下文章